| # | Time | Username | Problem | Language | Result | Execution time | Memory | 
|---|---|---|---|---|---|---|---|
| 239837 | MrRobot_28 | Mate (COCI18_mate) | C++17 | 430 ms | 20472 KiB | 
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 time | Memory | Grader output | 
|---|---|---|---|---|
| Fetching results... | ||||
