Submission #656447

# Submission time Handle Problem Language Result Execution time Memory
656447 2022-11-07T13:37:32 Z thanh913 Anagramistica (COCI21_anagramistica) C++14
110 / 110
5 ms 6356 KB
#include <bits/stdc++.h>
using namespace std;

//types
#define ll long long
#define ld long double
#define pll pair<ll, ll>
#define pii pair<int, int>

#define fi first
#define se second
#define inf 0x3f3f3f3f

#define pw2(x) (1LL << (x))
#define getBit(x, y) ((x) & (1LL << (y)))

template<class X, class Y>
bool cmax(X &a, const Y &b) {
    return a < b ? a = b, 1 : 0;
}

template<class X, class Y>
bool cmin(X &a, const Y &b) {
    return a > b ? a = b, 1 : 0;
}

//lowercase 31, all 53
//(P/Q) % M = (P % M) * (Q^(M-2) % M)
//-------------------------------------------------------

const ld PI = 3.14159265359;
const ll N = 2e3+5, mod = 1e9+7;
ll n, m, x, a[N];
ll f2[N][N], f[N][N];
map<string, int> mp;

ll add(ll &x, ll y) {
    if ((x += y) > mod) x -= mod;
    return x;
}

ll mu(ll x, ll y) {
    if (!y) return 1;
    ll tmp = mu(x, y/2);
    tmp = tmp * tmp % mod;
    if (y % 2) return tmp * x % mod;
    return tmp;
}

ll fac[N], inv[N];

ll ckn(ll k, ll n) {
    if (k > n) return 0;
    return (fac[n] * inv[k] % mod * inv[n-k] % mod);
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    //freopen("test.inp", "r", stdin);
    //freopen("test.out", "w", stdout);
    cin >> n >> x;
    for (int i = 1; i <= n; i++) {
        string tmp; cin >> tmp;
        sort(tmp.begin(), tmp.end());
        mp[tmp]++;
    }
    for (auto u : mp) {
        a[++m] = u.se;
    }
    fac[0] = 1;
    for (int i = 1; i <= 2000; i++) {
        fac[i] = fac[i-1] * i % mod;
    }
    inv[2000] = mu(fac[2000], mod-2);
    for (int i = 1999; i >= 0; i--)  {
        inv[i] = inv[i+1] * (i+1) % mod;
    }

    f[0][0] = 1;
    for (int i = 1; i <= m; i++) {
        for (int j = 0; j <= x; j++)
            f[i][j] = f[i-1][j];
        for (int j = 1; j <= a[i]; j++) {
            ll cnt = j * (j-1) / 2, cnt2 = ckn(j, a[i]);
            for (int k = 0; k <= x - cnt; k++) {
                add(f[i][k+cnt], f[i-1][k] * cnt2 % mod);
            }
        }
    }
    cout << f[m][x];
}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 596 KB Output is correct
2 Correct 1 ms 1620 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 2 ms 1628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 336 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 1620 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 2 ms 1628 KB Output is correct
10 Correct 2 ms 2004 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 1236 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 5 ms 6356 KB Output is correct
17 Correct 3 ms 724 KB Output is correct
18 Correct 1 ms 1364 KB Output is correct