Submission #391949

# Submission time Handle Problem Language Result Execution time Memory
391949 2021-04-20T08:34:57 Z Jarif_Rahman Anagramistica (COCI21_anagramistica) C++17
110 / 110
40 ms 50736 KB
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
const ll md = 1e9 + 7;
struct combination{
    vector<vector<ll>> ncr;
    combination(int k){
        ncr = vector<vector<ll>>(k, vector<ll>(k, 0));
        for(int i = 0; i < k; i++) ncr[i][0] = 1;
        for(int i = 1; i < k; i++) for(int j = 1; j <= i; j++) ncr[i][j] = (ncr[i-1][j-1] + ncr[i-1][j])%md;
    }
};
inline ll pw(ll x, ll p){
    if(p == 0) return 1;
    if(p%2 == 0) return((x*x)%md,  p/2);
    return (pw(x, p-1)*x)%md;
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, k; cin >> n >> k;
    vector<str> v(n);
    for(str &s: v){
        cin >> s;
        sort(s.begin(), s.end());
    }
    sort(v.begin(), v.end());
    vector<int> sth;
    ll ans = 0, p = 0;
    for(int i = 0; i < n; i++){
        if(i != 0 && v[i] == v[i-1]){
            sth.back()++;
        }
        else{
            sth.pb(1);
        }
    }
    n = sth.size();
    combination cmb(2500);
    vector<vector<ll>> dp(n, vector<ll>(k+1, 0));
    for(int i = n-1; i >= 0; i--){
        ll c = sth[i];
        for(int ki = 0; ki <= k; ki++){
            for(int j = 0; j <= c; j++){
                ll x = j;
                x = x*(x-1)/2;
                if(x > ki) continue;
                if(x == ki && i == n-1){
                    dp[i][ki]+=cmb.ncr[c][j];
                    dp[i][ki]%=md;
                    continue;
                }
                if(i == n-1) continue;
                dp[i][ki]+=(cmb.ncr[c][j]*dp[i+1][ki-x])%md;
                dp[i][ki]%=md;
            }
        }
    }
    ans = dp[0][k];
    ans%=md;
    cout << ans << "\n";
}

Compilation message

anagramistica.cpp: In function 'll pw(ll, ll)':
anagramistica.cpp:19:30: warning: left operand of comma operator has no effect [-Wunused-value]
   19 |     if(p%2 == 0) return((x*x)%md,  p/2);
      |                         ~~~~~^~~
anagramistica.cpp: In function 'int main()':
anagramistica.cpp:33:17: warning: unused variable 'p' [-Wunused-variable]
   33 |     ll ans = 0, p = 0;
      |                 ^
# Verdict Execution time Memory Grader output
1 Correct 33 ms 49228 KB Output is correct
2 Correct 32 ms 49296 KB Output is correct
3 Correct 33 ms 49340 KB Output is correct
4 Correct 33 ms 49220 KB Output is correct
5 Correct 33 ms 49220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 49356 KB Output is correct
2 Correct 34 ms 49344 KB Output is correct
3 Correct 34 ms 49416 KB Output is correct
4 Correct 34 ms 49356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 49228 KB Output is correct
2 Correct 32 ms 49296 KB Output is correct
3 Correct 33 ms 49340 KB Output is correct
4 Correct 33 ms 49220 KB Output is correct
5 Correct 33 ms 49220 KB Output is correct
6 Correct 34 ms 49356 KB Output is correct
7 Correct 34 ms 49344 KB Output is correct
8 Correct 34 ms 49416 KB Output is correct
9 Correct 34 ms 49356 KB Output is correct
10 Correct 35 ms 49728 KB Output is correct
11 Correct 34 ms 49356 KB Output is correct
12 Correct 36 ms 49348 KB Output is correct
13 Correct 35 ms 49300 KB Output is correct
14 Correct 36 ms 49444 KB Output is correct
15 Correct 38 ms 49420 KB Output is correct
16 Correct 39 ms 50736 KB Output is correct
17 Correct 40 ms 49544 KB Output is correct
18 Correct 35 ms 49392 KB Output is correct