ACM-ICPC 2018 焦作赛区网络预赛-L- Poor God Water

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/C_13579/article/details/82778204

God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him that some sequence of eating will make them poisonous.

Every hour, God Water will eat one kind of food among meat, fish and chocolate. If there are 33 continuous hours when he eats only one kind of food, he will be unhappy. Besides, if there are 33 continuous hours when he eats all kinds of those, with chocolate at the middle hour, it will be dangerous. Moreover, if there are 33 continuous hours when he eats meat or fish at the middle hour, with chocolate at other two hours, it will also be dangerous.

Now, you are the doctor. Can you find out how many different kinds of diet that can make God Water happy and safe during NN hours? Two kinds of diet are considered the same if they share the same kind of food at the same hour. The answer may be very large, so you only need to give out the answer module 10000000071000000007.

Input

The fist line puts an integer TT that shows the number of test cases. (T \le 1000T≤1000)

Each of the next TT lines contains an integer NN that shows the number of hours. (1 \le N \le 10^{10}1≤N≤1010)

Output

For each test case, output a single line containing the answer.

样例输入复制

3
3
4
15

样例输出复制

20
46
435170

地址:https://nanti.jisuanke.com/t/31721

思路:利用ac自动机和快速幂的一道模板题(我也不知道怎么求解)

Code :

扫描二维码关注公众号,回复: 3395472 查看本文章
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
typedef long long LL;

const int MAX_M=15;
const int max_tot = 105; 	//最大编号 
const int max_size = 4;	//组成元素个数##
const int mod = 1e5;
char s[15];
int chr[30];
struct mac {
	LL a[max_tot][max_tot];
	int len;
	mac() {
		len = 0;
		memset(a, 0, sizeof(a));
	}
	mac operator*(const mac &c)const {
		mac t;
		t.len = len;
		for (int i = 0; i < len; ++i)
			for (int j = 0; j < len; ++j) {
				t.a[i][j] = 0;
				for (int k = 0; k < len; ++k)
					t.a[i][j] += a[i][k] * c.a[k][j];
				t.a[i][j] %= mod;
			}
		return t;
	}
};

struct AC {
	int trie[max_tot][max_size];
	int val[max_tot];
	int fail[max_tot];
	int size;
	
	void Clear()
	{
		memset(trie, 0, sizeof(trie));
		memset(val, 0, sizeof(val));
		memset(fail,0,sizeof(fail));
		size = 1;
	}
	
	void insert(char *str)
	{
		int k = 0;
		for (int i = 0; str[i]; ++i) 
		{
			int x = chr[str[i]-'A'];
			if (!trie[k][x]){
				trie[k][x] = size++;
			}
			k = trie[k][x];
		}
		val[k] = 1;
	}

	void GetFail()
	{
		queue<int> Q;
		for (int i = 0; i < max_size; ++i)
			if(trie[0][i])	Q.push(trie[0][i]);
		while (!Q.empty()) {
			int r=Q.front(),k;	Q.pop();
			if(val[fail[r]])	val[r]=1;
			for (int i = 0; i < max_size; ++i) 
			{
				k = trie[r][i];
				if (k) {
					Q.push(k);
					fail[k] = trie[fail[r]][i];
				}else	trie[r][i] = trie[fail[r]][i];
			}
		}
	}
}ac;

mac Pow(mac a, LL b){
	mac ans;
	ans.len = a.len;
	for (int i = 0; i < a.len; ++i)
		ans.a[i][i] = 1;
	while (b) {
		if (b & 1)	ans = ans*a;
		a = a * a;
		b >>= 1;
	}
	return ans;
}

int main()
{
	chr['A'-'A']=0;	chr['C'-'A']=1;	chr['T'-'A']=2;	chr['G'-'A']=3;
	LL m,n;
    while(~scanf("%lld%lld",&m,&n)){
		ac.Clear();
		for (int i = 0; i < m; ++i)
		{
			scanf("%s",s);
			ac.insert(s);
		}
		ac.GetFail();
		mac ans=mac();
		ans.len = ac.size;
		for (int i = 0; i < ac.size; ++i) 
			for (int j = 0; j < max_size; ++j)
			{
				int u = ac.trie[i][j];
				if (!ac.val[u])	++ans.a[i][u];
			}
		ans = Pow(ans, n);
		LL sum = 0;
		for (int i = 0; i < max_tot; ++i)
			sum = (sum + ans.a[0][i]) % mod;
		printf("%lld\n",sum);
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/C_13579/article/details/82778204