Happy Necklace(找规律+矩阵快速幂)

6064: Happy Necklace 分享至QQ空间

时间限制(普通/Java):1000MS/3000MS     内存限制:65536KByte
总提交: 28            测试通过:11

描述

 

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 109+7.
Note: The necklace is a single string, {not a circle}.

输入

 

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≤1018), denoting the number of beads on the necklace.

输出

 

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

扫描二维码关注公众号,回复: 7668988 查看本文章

样例输入

样例输出

3
4

解题思路:  题目意思 在长度内的每个素数段红色的珠子要大于等于蓝色的珠子;

保证长度为2和3的的长度即可 后面的都可以用这两个长度表示出来    在前一个状态下可以直接加1  要加0的话只能前面两个数为11  手动模拟(或者打表可以得到规律) 

f[n]=f[n-1]+f[n-3];     应为n很大  矩阵快速幂是必然的

f[4] f[3] f[2]

 6     4    2

构造出矩阵

1 1 0

0 0 1

1 0 0 

具体看代码

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 #define mod(x) ((x)%MOD)
 4 using namespace std;
 5 
 6 const int maxn=3;
 7 const int MOD=1e9+7;
 8 int t;
 9 ll n;
10 
11 struct mat{
12     int m[maxn][maxn];
13 }unit;
14 
15 void init_unit(){
16     for(int i=0;i<maxn;i++){
17         unit.m[i][i]=1;
18     }
19     return;
20 }
21 
22 mat operator*(mat a,mat b){
23     mat ret;
24     ll x;
25     for(int i=0;i<maxn;i++)
26     for(int j=0;j<maxn;j++){
27         x=0;
28         for(int k=0;k<maxn;k++){
29             x+=mod(1LL*a.m[i][k]*b.m[k][j]);
30         }
31         ret.m[i][j]=mod(x);
32     }   //在自定义的乘里面mod
33     return ret;
34 }
35 
36 mat pow_mat(mat a,ll m){
37     mat ret=unit;
38     while(m){
39         if(m&1) ret=ret*a;
40         a=a*a;
41         m>>=1;
42     }
43     return ret;
44 
45 }
46 
47 int main(){
48     ios::sync_with_stdio(false);
49     init_unit();
50     cin>>t;
51     while(t--){
52         cin>>n;
53         mat a,b;
54         a.m[0][0]=6,a.m[0][1]=4,a.m[0][2]=3;
55 
56         b.m[0][0]=1,b.m[0][1]=1,b.m[0][2]=0;
57         b.m[1][0]=0,b.m[1][1]=0,b.m[1][2]=1;
58         b.m[2][0]=1,b.m[2][1]=0,b.m[2][2]=0;
59         if(n==2) cout << 3 << endl;
60         else if(n==3) cout << 4 << endl;
61         else if(n==4) cout << 6 << endl;
62         else{
63             b=pow_mat(b,n-4);
64             a=a*b;
65             cout << a.m[0][0] << endl;
66         }
67     }
68     return 0;
69 
70 
71 
72 
73 }
View Code

猜你喜欢

转载自www.cnblogs.com/qq-1585047819/p/11755681.html