답안 #239837

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
239837 2020-06-17T11:30:52 Z MrRobot_28 Mate (COCI18_mate) C++17
100 / 100
430 ms 20472 KB
#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;
}	
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 896 KB Output is correct
2 Correct 8 ms 768 KB Output is correct
3 Correct 8 ms 744 KB Output is correct
4 Correct 11 ms 896 KB Output is correct
5 Correct 35 ms 3296 KB Output is correct
6 Correct 50 ms 3392 KB Output is correct
7 Correct 35 ms 3064 KB Output is correct
8 Correct 29 ms 2936 KB Output is correct
9 Correct 428 ms 20344 KB Output is correct
10 Correct 430 ms 20472 KB Output is correct