제출 #873918

#제출 시각아이디문제언어결과실행 시간메모리
873918noiaintAnagramistica (COCI21_anagramistica)C++17
110 / 110
20 ms22360 KiB
#include <bits/stdc++.h>
 
using namespace std;
 
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()
 
#define getbit(x, i) (((x) >> (i)) & 1)
#define bit(x) (1 << (x))
#define popcount __builtin_popcountll
 
mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
long long rand(long long l, long long r) {
    return l + rd() % (r - l + 1);
}
 
const int N = 2e3 + 5;
const int mod = 1e9 + 7; // 998244353;
const int lg = 25; // lg + 1
const int inf = 1e9; // INT_MAX;
const long long llinf = 1e18; // LLONG_MAX;
 
template<class X, class Y>
bool minimize(X &a, Y b) {
    return a > b ? (a = b, true) : false;
}
 
template<class X, class Y>
bool maximize(X &a, Y b) {
    return a < b ? (a = b, true) : false;
}
 
template<class X, class Y>
bool add(X &a, Y b) {
    a += b;
    while (a >= mod) a -= mod;
    while (a < 0) a += mod;
    return true;
}
 
int n, k;
unordered_map<string, int> rec;
int c[N][N];
int f[N][N];
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
 
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) {
        string s;
        cin >> s;
        sort(all(s));
        rec[s]++;
    }
 
    for (int i = 1; i <= n; ++i) {
        c[0][i] = c[i][i] = 1;
        for (int j = 1; j < i; ++j) {
            add(c[j][i], c[j - 1][i - 1]);
            add(c[j][i], c[j][i - 1]);
        }
    }
 
    vector<int> words;
    for (auto [s, i] : rec) words.push_back(i);
    f[0][0] = 1;
 
    for (int i = 1; i <= (int) words.size(); ++i) {
        for (int cnt = 0; cnt <= words[i - 1]; ++cnt) {
            for (int j = 0; j <= k; ++j) {
                int offset = max(0, cnt * (cnt - 1) / 2);
                if (j - offset >= 0) {
                    add(f[i][j], 1LL * c[cnt][words[i - 1]] * f[i - 1][j - offset] % mod);
                }
            }
        }
    }
 
    cout << f[(int) words.size()][k];
 
    return 0;
}
 
/*
 
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...