Submission #239837

#TimeUsernameProblemLanguageResultExecution timeMemory
239837MrRobot_28Mate (COCI18_mate)C++17
100 / 100
430 ms20472 KiB
#include <bits/stdc++.h> using namespace std; #define int long long const int const1 = 1e9 + 7; int power(int a, int b) { if(b == 0) { return 1; } else if(b % 2 == 0) { int t = power(a, b / 2); return t * t % const1; } else { int t = power(a, b / 2); t = t * t % const1; return t * a % const1; } } signed main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); string s; cin >> s; int n = s.size(); int ans[n + 1][26][26]; for(int i = 1; i <= n; i++) { for(int j = 0; j < 26; j++) { for(int k = 0; k < 26; k++) { ans[i][j][k] = 0; } } } vector <int> fact(n + 1), fact1(n + 1); fact[0] = 1; for(int i = 1; i <= n; i++) { fact[i] = fact[i - 1] * i % const1; } fact1[n] = power(fact[n], const1 - 2); for(int i = n - 1; i >= 0; i--){ fact1[i] = fact1[i + 1] * (i + 1) % const1; } vector <vector <int> > post(n + 1, vector <int> (26, 0)); for(int i = n - 1; i >= 0; i--) { for(int j = 0; j < 26; j++) { post[i][j] = post[i + 1][j]; } post[i][s[i] - 'a']++; } for(int i = 0; i < n - 1; i++) { for(int len = 0; len <= i; len++) { int p = fact[i] * fact1[len] % const1 * fact1[i- len] % const1; for(int j = 0; j < 26; j++) { ans[len + 2][s[i] - 'a'][j] += p * post[i + 1][j] % const1; if(ans[len + 2][s[i] - 'a'][j] >= const1) { ans[len + 2][s[i] - 'a'][j] -= const1; } } } } int q; cin >> q; while(q--) { int len; cin >> len; char t1, t2; cin >> t1 >> t2; cout << ans[len][t1 - 'a'][t2 - 'a'] << "\n"; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...