ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

hud 6030 Happy Necklace (矩阵快速幂)

2019-08-05 19:06:42  阅读:352  来源: 互联网

标签:hud beads temp int number lom necklace Necklace 6030


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有