hdu 6030 Happy Necklace (递推,矩阵快速幂)

本文探讨了HappyNecklace问题,即寻找特定条件下不同项链的数量。通过将问题转化为字符串匹配,并采用矩阵快速幂的方法求解,给出了具体的算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

规律呢。。。

就是

把红蓝转化为 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);
    }
}





评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值