题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=6030
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 +.
Note: The necklace is a single string, {not a circle}. Input
The first line of the input contains an integer T(≤T≤), denoting the number of test cases.
For each test case, there is a single line containing an integer n(≤n≤), 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 +. Sample Input Sample Output Source
2017中国大学生程序设计竞赛 - 女生专场
题目大意:有n个珠子,有红蓝两种珠子 符合下列两个条件的珠子排列方法有多少种?
*******在每串手链种,长度为素数的子串种,红色珠子要不小于蓝色珠子
分析:找到规律:f[i] = f[i-1]+f[i-3];因为n很大 可以用矩阵快速幂写
初始矩阵为2 3 4 转移矩阵为0 0 1
1 0 0
0 1 1
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include <vector>
#include<iostream>
using namespace std;
#define N 50005
#define INF 0x3f3f3f3f
#define LL long long
#define mod 1000000007
struct node
{
LL a[][];
};
node mul(node a,node b)
{
node te;
memset(te.a,,sizeof(te.a));
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
for(int k=;k<;k++)
{
te.a[i][j]+=(a.a[i][k]*b.a[k][j])%mod;
}
}
}
return te;
}
LL quick(LL n)
{
if(n<)
{
int num[]={,,,};
return num[n];
}
node b,team;
b.a[][] = ;b.a[][] = ;b.a[][] = ;
b.a[][] = ;b.a[][] = b.a[][] = ;
b.a[][] = ;b.a[][] = b.a[][] = ;
memset(team.a,,sizeof(team.a));
for(int i=;i<;i++)
team.a[i][i] = ;
LL k = n-;
while(k)
{
if(k&)
team = mul(team,b);
b = mul(b,b);
k = k/;
}
return (team.a[][]*+team.a[][]*+team.a[][]*)%mod;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
LL n;
scanf("%lld",&n);
printf("%lld\n",quick(n));
}
return 0;
}