HDU 6030(矩阵快速幂+规律)

题目描述:

Happy Necklace

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


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

    题目描述:你有一个有n个颜色为红色或蓝色的珠子的项链,你可以将项链截断成长度为素数的珠子,如果截取出的一段红色的珠子的个数大于蓝色的珠子,则成为good,问一共有多少种good的可能性。
    比赛的时候,当拿到这道题的时候,完全一脸懵逼。打了好一会才意识到可以打表,故打只,可以发现
    n=2,res=3;n=3,res=4;n=4,res=6;n=5,res=9;
    故有递推式 An=An-1+An-3;

    赛后问了下师兄,发现正确的推断方法:
    

    如果用a表示红色,用b表示蓝色。题意明显可以看出只需要管长度2和3的连续序列是否符合!

    如果以b结尾,那么下一个必须是a,或者加个aab就可以了!

    所以同理就可以推出递推式An=An-1+An-3;
    看一下题目的数据范围,n最大1e18,因此常规O(n)的递推显然不可行,因此直接上O(logn)的矩阵快速幂。

    发现递推式是四阶的递推式,故所得的常数矩阵应该是四维的。之后只需带入矩阵快速幂模板即可。
    
#include <bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
typedef long long ll;
struct martix{
    ll mo[4][4];
    martix(){
        memset(mo,0,sizeof(mo));
    }
};
martix mul(martix a,martix b){
    martix c;
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            for(int k=0;k<4;k++){
                c.mo[i][j]=(c.mo[i][j]+a.mo[i][k]*b.mo[k][j])%mod;
            }
        }
    }
    return c;
}
ll powmod(martix a,ll n){
    martix T;
    for(int i=0;i<4;i++){
        T.mo[i][i]=1;
    }
    while(n){
        if(n&1) T=mul(a,T);
        n>>=1;
        a=mul(a,a);
    }
    return (T.mo[0][0]*6+T.mo[0][1]*4+T.mo[0][2]*3)%mod;
}
int main()
{
    ll t;
    cin>>t;
    while(t--){
        ll n;
        cin>>n;
        int ans[5]={0,0,3,4,6};
        if(n<=4){
            cout<<ans[n]%mod<<endl;
        }
        else{
            martix q;
            q.mo[0][0]=q.mo[0][2]=q.mo[3][2]=q.mo[1][0]=q.mo[2][1]=q.mo[3][2]=1;
            cout<<powmod(q,n-4)%mod<<endl;
        }
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值