HDU-6030 Happy Necklace

Happy Necklace

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 131072/131072 K (Java/Others)

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 10^9+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≤10^18), 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 10^9+7.

Sample Input

2
2
3

Sample Output

3
4

Reference Code

#include <cstdio>
#include <cstring>
const int MOD=1e9+7;
typedef long long ll;
struct Matrix{
    ll matrix[3][3];
    Matrix (int x=0){
        memset(matrix,0,sizeof(matrix));
        if (x==1){
            for (int i=0;i<3;++i) matrix[i][i]=1;
        }
    }
    Matrix operator*(const Matrix &oth)const{
        Matrix res;
        for (int i=0;i<3;++i){
            for (int j=0;j<3;++j){
                ll tmp=0;
                for (int k=0;k<3;++k){
                    tmp=(tmp+matrix[i][k]*oth.matrix[k][j])%MOD;
                }
                res.matrix[i][j]=tmp;
            }
        }
        return res;
    }
};
Matrix qp(Matrix n,ll k){
    Matrix res(1);
    while (k){
        if (k&1) res=res*n;
        n=n*n;
        k>>=1;
    }
    return res;
}
ll solve(int f1,int f2,int f3,ll k){
    Matrix A;
    A.matrix[0][0]=1;
    A.matrix[0][2]=1;
    A.matrix[1][0]=1;
    A.matrix[2][1]=1;
    if (k<=3) return k+1;
    Matrix res=qp(A,k-3);
    return (res.matrix[0][0]*f3%MOD+res.matrix[0][1]*f2%MOD+res.matrix[0][2]*f1)%MOD;
}
int main(){
    int t;
    scanf("%d",&t);
    while (t--){
        ll n;
        scanf("%lld",&n);
        printf("%lld\n",solve(2,3,4,n));
    }
    return 0;
}

Tips

题意:
一串项链(非环),上有蓝红两色宝石,若蓝色之间不能紧挨着且蓝色之间不能只隔着一个红色,求一共的方法。

题解:
设数组a[]b[]c[]分别为后面两个字母为rrbrrb的字符串的数量。可以得到:
a [ i ] = a [ i − 1 ] + c [ i − 1 ] b [ i ] = a [ i − 1 ] c [ i ] = b [ i − 1 ] \begin{aligned}a[i] &amp;= a[i-1]+c[i-1]\\ b[i] &amp;= a[i-1]\\ c[i] &amp;= b[i-1]\end{aligned} a[i]b[i]c[i]=a[i1]+c[i1]=a[i1]=b[i1]

因而有:
s [ n ] = a [ n ] + b [ n ] + c [ n ] = 2 ∗ a [ i − 1 ] + c [ i − 1 ] + b [ i − 1 ] = s [ n − 1 ] + a [ i − 1 ] = s [ n − 1 ] + a [ i − 2 ] + c [ i − 2 ] = s [ n − 1 ] + a [ i − 3 ] + b [ i − 3 ] + c [ i − 3 ] = s [ n − 1 ] + s [ n − 3 ] \begin{aligned}s[n]&amp;=a[n]+b[n]+c[n]\\ &amp;=2*a[i-1]+c[i-1]+b[i-1]\\ &amp;=s[n-1]+a[i-1]\\ &amp;=s[n-1]+a[i-2]+c[i-2]\\ &amp;=s[n-1]+a[i-3]+b[i-3]+c[i-3]\\ &amp;=s[n-1]+s[n-3]\end{aligned} s[n]=a[n]+b[n]+c[n]=2a[i1]+c[i1]+b[i1]=s[n1]+a[i1]=s[n1]+a[i2]+c[i2]=s[n1]+a[i3]+b[i3]+c[i3]=s[n1]+s[n3]

得到递归式: s [ n ] = s [ n − 1 ] + s [ n − 3 ] s[n]=s[n-1]+s[n-3] s[n]=s[n1]+s[n3]
然后即可用矩阵快速幂求解。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值