Hdu 6030 Happy Necklace【Dp+矩阵快速幂】

来源:互联网 发布:决策树算法 编辑:程序博客网 时间:2024/06/04 19:28

Happy Necklace

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 513    Accepted Submission(s): 223


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}.
 

Input
The first line of the input contains an integer T(1T10000), denoting the number of test cases.
For each test case, there is a single line containing an integer n(2n1018), 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
223
 

Sample Output
34
 

Source
2017中国大学生程序设计竞赛 - 女生专场

题目大意:


现在需要将两种颜色珠子排列成一排(红色和蓝色)。现在要求这串珠子中,任意一个点作为起点,任意素数长度中,红色的珠子的数量不能少蓝色珠子的数量。

问有几种分配方式。


思路:


设定dp【i】表示长度为i的串有几种排列方式。

显然,这样的串可以在(i-1)长度的串中在末尾添加一个红色珠子而来,那么有:Dp【i】+=Dp【i-1】;

接下来考虑,长度为2中的串肯定不能有两个全蓝的情况,那么也就是要求现在不能有两个蓝色的珠子连在一起。

再考虑,长度为3中的串肯定不能有两个蓝的情况,那么也就是说两个蓝色的珠子还不能间隔一个排列(蓝红蓝);

再考虑,长度为5中的串肯定不能有三个蓝的情况,但是假设我们此时满足长度为3没有两个蓝的情况,我们知道,长度为5的串中出现3个蓝色的是绝对不可能的。

再考虑,长度为7中的串肯定不能有四个蓝的情况,但是我们假设此时满足长度为3没有两个蓝的情况,我们也知道,长度为7的串中出现4个蓝色的也是绝对不可能的。

依次类推,那么我们只要两个蓝色的间隔大于2即可。(蓝红红蓝);

那么就有:Dp【i】+=Dp【i-3】;


综上,Dp【i】=Dp【i-1】+Dp【i-3】;


n比较大,我们矩阵快速幂优化递推速度即可。


Ac代码:

#include<stdio.h>#include<string.h>using namespace std;#define ll __int64#define mod 1000000007typedef struct Matrix{    ll mat[4][4];}matrix;Matrix A,B,tmp;Matrix matrix_mul(matrix a,matrix b){    matrix c;    memset(c.mat,0,sizeof(c.mat));    int i,j,k;    for(int i=0;i<3;i++)    {        for(int j=0;j<3;j++)        {            for(int k=0;k<3;k++)            {                c.mat[i][j]+=a.mat[i][k]*b.mat[k][j];                c.mat[i][j]%=mod;            }        }    }    return c;}Matrix matrix_quick_power(matrix a,ll k){    matrix b;    memset(b.mat,0,sizeof(b.mat));    for(int i=0;i<3;i++)    b.mat[i][i]=1;//单位矩阵b    while(k)    {        if(k%2==1)        {            b=matrix_mul(a,b);            k-=1;        }        else        {            a=matrix_mul(a,a);            k/=2;        }    }    return b;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        ll n;        scanf("%I64d",&n);        memset(A.mat,0,sizeof(A.mat));        memset(B.mat,0,sizeof(B.mat));        memset(tmp.mat,0,sizeof(tmp.mat));        if(n<=3)        {            if(n==1)printf("2\n");            if(n==2)printf("3\n");            if(n==3)printf("4\n");            continue;        }        tmp.mat[0][0]=4;        tmp.mat[1][0]=3;        tmp.mat[2][0]=2;        A.mat[0][0]=1;A.mat[0][1]=0;A.mat[0][2]=1;        A.mat[1][0]=1;A.mat[1][1]=0;A.mat[1][2]=0;        A.mat[2][0]=0;A.mat[2][1]=1;A.mat[2][2]=0;        B=matrix_quick_power(A,n-3);        B=matrix_mul(B,tmp);        printf("%I64d\n",B.mat[0][0]);    }}













原创粉丝点击