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

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
  
  
2 2 3
 

Sample Output
  
  
3 4
 

Source

题目大意:


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

问有几种分配方式。


思路:


设定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 1000000007
typedef 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]);
    }
}













  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值