This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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++) {
| ~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |