답안 #483872

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
483872 2021-11-01T06:58:52 Z Sohsoh84 Anagramistica (COCI21_anagramistica) C++17
110 / 110
11 ms 6400 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;

#define all(x)                      (x).begin(),(x).end()
#define X                           first
#define Y                           second
#define sep                         ' '
#define endl                        '\n'
#define debug(x)                    cerr << #x << ": " <<  x << endl;
#define int 			    ll

const ll MAXN = 3e3 + 10;
const ll INF = 8e18;
const ll MOD = 1e9 + 7; // 998244353;

ll poww(ll a, ll b, ll md) {
	return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md));
}
 
ll A[MAXN], n, k, fact[MAXN], inv[MAXN], dp[MAXN][MAXN];

inline ll C(int k, int n) {
	return fact[n] * inv[k] % MOD * inv[n - k] % MOD;
}

int32_t main() {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	fact[0] = inv[0] = 1;
	for (int i = 1; i < MAXN; i++) fact[i] = i * fact[i - 1] % MOD, inv[i] = poww(fact[i], MOD - 2, MOD);
	
	cin >> n >> k;
	vector<string> v;
	for (int i = 0; i < n; i++) {
		string s;
		cin >> s;
		sort(all(s));
		v.push_back(s);
	}

	sort(all(v));
	int n = 0;
	for (int i = 0; i < v.size(); i++) {
		if (i == 0 || v[i] != v[i - 1]) n++;
		A[n]++;
	}

	dp[0][0] = 1;
	for (int i = 1; i <= n; i++) {
		for (int j = 0; j <= k; j++) {
			for (int t = 0; t <= A[i]; t++) {
				ll c2 = t * (t - 1) / 2;
				ll c = C(t, A[i]);
				if (c2 > j) continue;
				
				dp[i][j] += dp[i - 1][j - c2] * c % MOD;
				dp[i][j] %= MOD;
			}
		}
	}

	cout << dp[n][k] << endl;
	return 0;
}

Compilation message

anagramistica.cpp: In function 'int32_t main()':
anagramistica.cpp:46:20: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::__cxx11::basic_string<char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |  for (int i = 0; i < v.size(); i++) {
      |                  ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 2 ms 356 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 716 KB Output is correct
2 Correct 3 ms 1740 KB Output is correct
3 Correct 3 ms 588 KB Output is correct
4 Correct 3 ms 1740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 2 ms 356 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 2 ms 716 KB Output is correct
7 Correct 3 ms 1740 KB Output is correct
8 Correct 3 ms 588 KB Output is correct
9 Correct 3 ms 1740 KB Output is correct
10 Correct 4 ms 2124 KB Output is correct
11 Correct 2 ms 460 KB Output is correct
12 Correct 2 ms 588 KB Output is correct
13 Correct 3 ms 588 KB Output is correct
14 Correct 4 ms 1420 KB Output is correct
15 Correct 3 ms 588 KB Output is correct
16 Correct 11 ms 6400 KB Output is correct
17 Correct 8 ms 896 KB Output is correct
18 Correct 3 ms 1484 KB Output is correct