Happy Necklace
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 660 Accepted Submission(s): 299
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.
Note: The necklace is a single string, {not a circle}.
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}.
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.
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
Source
规律呢。。。
就是
把红蓝转化为 1 0
每个串 的末尾只能是 1 | 0
由n-1 长度的串可以添加一个 1就可以组成 n长度的串
关键是添加0 的。。
0想要添加0 倒数第二个不能是0 倒数第三个不能是0
所以就直接添加110
f【n】=f【n-1】+f【n-2】;
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
long long n,k,m;
const long long N = 1e9+7;
struct Matris///矩阵
{
long long r,c;
long long mat[3][3];
};
Matris Matris_Mul(Matris a,Matris b)
{
Matris tem;///temporary(暂时变量(背背英语勿怪))
memset(tem.mat,0,sizeof(tem.mat));
for(long long i=0; i<3; i++)
{
for(long long j=0; j<3; j++)
{
for(long long k=0; k<3; k++)
{
tem.mat[i][j]+=((a.mat[i][k])*(b.mat[k][j]%N));
tem.mat[i][j]%=N;
}
}
}
return tem;
}
long long Quick_Mi(long long n)
{
Matris res;
res.mat[0][0]=6,res.mat[0][1]=4,res.mat[0][2]=3;
res.mat[1][0]=0,res.mat[1][1]=0,res.mat[1][2]=0;
res.mat[2][0]=0,res.mat[2][1]=0,res.mat[2][2]=0;
Matris tmp;
tmp.mat[0][0]=1,tmp.mat[0][1]=1,tmp.mat[0][2]=0;
tmp.mat[1][0]=0,tmp.mat[1][1]=0,tmp.mat[1][2]=1;
tmp.mat[2][0]=1,tmp.mat[2][1]=0,tmp.mat[2][2]=0;
while(n)
{
if(n%2)
{
res=Matris_Mul(res,tmp);
}
tmp=Matris_Mul(tmp,tmp);
n/=2;
}
return res.mat[0][0]%N;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
long long n;
scanf("%lld",&n);
if(n==2)
printf("3\n");
else if(n==3)printf("4\n");
else if(n==4)
printf("6\n");
else printf("%lld\n",Quick_Mi(n-4));
}
}
///100000000000000000
本着买一送一的原则。。
送上一个打表的码子。。
#include<stdio.h>
#include<string.h>
int a[1003];
void dfs(int x,int pos)
{
if(pos==x+1)
{
for(int i=1; i<=x; i++)
{
printf("%d ",a[i]);
}
printf("\n");
return;
}
if(pos==1||(pos==2&&a[pos-1]!=0))
{
a[pos]=0;
dfs(x,pos+1);
a[pos]=1;
dfs(x,pos+1);
return;
}
if(a[pos-2]!=0&&a[pos-1]!=0)
{
a[pos]=0;
dfs(x,pos+1);
a[pos]=1;
dfs(x,pos+1);
return;
}
a[pos]=1;
dfs(x,pos+1);
}
int main()
{
for(int i=2; i<=10; i++)
{
printf(" i : %d\n",i);
dfs(i,1);
}
}