#include <bits/stdc++.h>
using namespace std;
/*
1) Precalcular a frequência de sufixo de cada letra -> O(n)
2) Precalcular x^(MOD-2) (1 <= x <= n) -> O(n log MOD)
3) Precalcular x escolhe y (y <= x <= n) -> O(n²)
4) Solve! -> O(n²)
*/
const int MAXN = 2e3+10;
const int MOD = 1e9+7;
typedef long long ll;
int n, q;
ll freq[MAXN][30], choose[MAXN][MAXN], pot[MAXN];
ll ans[MAXN][30][30];
string s;
ll Pot(ll b, ll e)
{
if (e == 0LL) return 1LL;
ll x = Pot(b, e/2);
if (e%2 == 0LL) return (x*x)%MOD;
else return (b*x*x)%MOD;
}
void pre_freq(void)
{
for (int i = n; i >= 1; i--)
{
for (int j = 1; j <= 26; j++)
{
if ((int) s[i-1]-(int)'a'+1 == j)
freq[i][j] = freq[i+1][j]+1LL;
else
freq[i][j] = freq[i+1][j];
}
}
}
void pre_pot(void)
{
for (ll i = 0LL; i <= n; i++)
pot[i] = Pot(i, MOD-2)%MOD;
}
void pre_choose(void)
{
for (int i = 0; i <= n; i++)
{
choose[i][0] = 1LL;
for (int j = 1; j <= i; j++)
{
choose[i][j] = (choose[i][j-1]*(i-j+1))%MOD;
choose[i][j] = (choose[i][j]*pot[j])%MOD;
}
}
}
int main(void)
{
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> s;
n = s.size();
pre_freq();
pre_pot();
pre_choose();
for (int i = 1; i <= n; i++)
{
for (int j = 2; j <= i+1; j++)
{
for (int l = 1; l <= 26; l++)
{
int a1 = (int) s[i-1]-(int)'a'+1;
ans[j][a1][l] = (ans[j][a1][l]+freq[i+1][l]*choose[i-1][j-2])%MOD;
}
}
}
cin >> q;
while (q--)
{
int m;
char a1, a2;
cin >> m >> a1 >> a2;
int b1 = (int) a1-(int)'a'+1, b2 = (int)a2-(int)'a'+1;
cout << ans[m][b1][b2] << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
1016 KB |
Output isn't correct |
2 |
Incorrect |
6 ms |
1016 KB |
Output isn't correct |
3 |
Incorrect |
7 ms |
1084 KB |
Output isn't correct |
4 |
Incorrect |
8 ms |
1152 KB |
Output isn't correct |
5 |
Incorrect |
33 ms |
3840 KB |
Output isn't correct |
6 |
Incorrect |
38 ms |
3964 KB |
Output isn't correct |
7 |
Incorrect |
30 ms |
3964 KB |
Output isn't correct |
8 |
Incorrect |
28 ms |
3964 KB |
Output isn't correct |
9 |
Incorrect |
528 ms |
42612 KB |
Output isn't correct |
10 |
Incorrect |
527 ms |
42724 KB |
Output isn't correct |