其他分享
首页 > 其他分享> > hud 6030 Happy Necklace (矩阵快速幂)

hud 6030 Happy Necklace (矩阵快速幂)

作者:互联网

Happy Necklace

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2402    Accepted Submission(s): 984


 

Problem Description

Little Q wants to buy a necklace for his girlfriend. Necklaces are single strings composed of multiple red and blue beads.
Little Q desperately wants to impress his girlfriend, he knows that she will like the necklace only if for every prime length continuous subsequence in the necklace, the number of red beads is not less than the number of blue beads.
Now Little Q wants to buy a necklace with exactly n  beads. He wants to know the number of different necklaces that can make his girlfriend happy. Please write a program to help Little Q. Since the answer may be very large, please print the answer modulo 10 9 +7  .
Note: The necklace is a single string, {not a circle}.

 

 

Input

The first line of the input contains an integer T(1≤T≤10000)  , denoting the number of test cases.
For each test case, there is a single line containing an integer n(2≤n≤10 18 )  , denoting the number of beads on the necklace.

 

 

Output

For each test case, print a single line containing a single integer, denoting the answer modulo 10 9 +7  .

 

 

Sample Input

2 2 3

 

 

Sample Output

3 4

 

 

Source

2017中国大学生程序设计竞赛 - 女生专场

 


a(n) = a(n-1) + a(n-3)

手算前几项找规律

#include<bits/stdc++.h>

using namespace std;
using lom = long long;
const lom mod = 1e9 +7;
void matrix(lom A[3][3], lom B[3][3], lom C[3][3])
{
	lom temp[3][3]; 
	memset(temp,0,sizeof(temp));
	
	for(int i = 0; i < 3; i++)
	for(int j = 0; j < 3; j++)
	{
		for( int k = 0; k < 3; k++)
	//	for( int t = 0; t < 3; t++)
		temp[i][j] = ( temp[i][j] + A[i][k]*B[k][j]%mod ) % mod;
	//	cout<<endl;
	}
	
	
	memcpy(C,temp,sizeof(temp));//copy 
}

lom slove( lom n )
{
	if(n==0) return 3;
	if(n==1) return 4;
	
	lom ans[3][3] = {6,0,0,4,0,0,3,0,0};
	lom   A[3][3] = {1,0,1,1,0,0,0,1,0};
	
	while( n > 0 )
	{
		if( n & 1 ) matrix( A, ans, ans );
		n >>= 1;
		matrix( A, A, A );
	}

	return ans[2][0];
}

int main()
{
	lom n,t;
	cin >> t;
	while(t--)
	{
		cin >> n;
		cout << slove( n-2 ) <<endl;
	}
	
	return 0;
}











 

标签:hud,beads,temp,int,number,lom,necklace,Necklace,6030
来源: https://blog.csdn.net/qq_41431457/article/details/98507552