1. 程式人生 > >ACM-ICPC 2018 焦作賽區網路預賽-L- Poor God Water

ACM-ICPC 2018 焦作賽區網路預賽-L- Poor God Water

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

思路:利用ac自動機和快速冪的一道模板題(我也不知道怎麼求解)

Code :

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
using namespace std;
typedef long long LL;

const int max_tot = 55;
const int max_size = 3;
const LL mod = 1e9+7;
char s[8][4] = {"111","222","333","132","231","313","323"};
struct mac {
	LL a[30][30];
	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 = str[i]-'1';
			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();
			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()
{
	LL n,T;
	scanf("%lld",&T);
	while (T--) {
        scanf("%lld",&n);
		ac.Clear();
		for (int i = 0; i < 7; ++i)
			ac.insert(s[i]);
		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 < 22; ++i)
			sum = (sum + ans.a[0][i]) % mod;
		printf("%lld\n",sum);
	}
	return 0;
}