#include <bits/stdc++.h>
using namespace std;
const int maxm = 30;
const int maxn = 2010;
typedef long long ll;
const ll mod = 1000000007;
ll dp[maxm][maxm][maxn];
ll pchoose[maxn][maxn];
ll choose(ll n, ll k)
{
if (pchoose[n][k] >= 0) return pchoose[n][k];
return pchoose[n][k] = (choose(n-1, k) + choose(n-1, k-1)) % mod;
}
void init()
{
memset(pchoose, -1, maxn*maxn*8);
for (int i = 0; i < maxn; i++) {
pchoose[i][0] = 1;
pchoose[i][i] = 1;
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
string s;
cin >> s;
int n = s.size();
init();
for (int i = 0; i < n-1; i++) {
for (int j = i+1; j < n; j++) {
for (int k = 0; k <= i; k++) {
dp[s[i]-'a'][s[j]-'a'][k+2] += choose(i, k);
dp[s[i]-'a'][s[j]-'a'][k+2] %= mod;
}
}
}
int q;
cin >> q;
while (q--) {
int num;
char a, b;
cin >> num >> a >> b;
cout << dp[a-'a'][b-'a'][num] << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
34040 KB |
Output is correct |
2 |
Correct |
32 ms |
34040 KB |
Output is correct |
3 |
Correct |
33 ms |
34296 KB |
Output is correct |
4 |
Correct |
36 ms |
34492 KB |
Output is correct |
5 |
Correct |
72 ms |
38160 KB |
Output is correct |
6 |
Correct |
73 ms |
39304 KB |
Output is correct |
7 |
Correct |
67 ms |
39984 KB |
Output is correct |
8 |
Correct |
61 ms |
40520 KB |
Output is correct |
9 |
Execution timed out |
2068 ms |
42312 KB |
Time limit exceeded |
10 |
Execution timed out |
2072 ms |
42312 KB |
Time limit exceeded |