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
109+7.
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≤1018), 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 109+7.
Sample Input
2
2
3
Sample Output
3
4
题目描述
一条n个珠子串成的链状项链,珠子只有蓝色和红色,如果保证项链的任意素数长度的子序列中,红色珠子个数大于等于蓝色珠子,问有多少种情况.
解题思路
找规律可以发现f[n]=f[n-1]+f[n-3].构建矩阵求解.
代码实现
#include<bits/stdc++.h>
#define IO ios::sync_with_stdio(false);\
cin.tie(0);\
cout.tie(0);
using namespace std;
typedef long long LL;
const int mod = 1e9+7;
#define ll long long
#define maxn 3
struct matrix
{
ll f[maxn][maxn];
}ans,ori;
matrix multi(const matrix &a,const matrix &b)
{
matrix c;
for(int i=0;i<maxn;i++)
for(int j=0;j<maxn;j++)
{
c.f[i][j]=0;
for(int k=0;k<maxn;k++)
{
c.f[i][j]=(c.f[i][j]+(a.f[i][k]%mod*(b.f[k][j]%mod)))%mod;
}
}
return c;
}
void quick_pow(ll num)
{
memset(ans.f,0,sizeof(ans.f));
ans.f[0][0]=ans.f[1][1]=ans.f[2][2]=1;
memset(ori.f,0,sizeof(ori.f));
ori.f[0][0]=ori.f[0][2]=ori.f[1][0]=ori.f[2][1]=1;
while(num>0)
{
if(num%2)
{
ans=multi(ans,ori);
}
ori=multi(ori,ori);
num/=2;
}
}
int main()
{
IO;
int T;
cin>>T;
while(T--)
{
ll n;
cin>>n;
if(n==2) cout<<"3"<<endl;
else if(n==3) cout<<"4"<<endl;
else if(n==4) cout<<"6"<<endl;
else
{
quick_pow(n-4);
cout<<(ans.f[0][0]*6%mod+ans.f[0][1]*4%mod+ans.f[0][2]*3%mod)%mod<<endl;
}
}
return 0;
}