【解题报告】hdu6030 Happy Necklace

Happy Necklace

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


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
 
【题目大意】用红蓝两种珠子,串成一条项链,
使得任意素数长度的区间都有红色珠子大于等于蓝色珠子,问有多少种串法
 
 
【思路】考虑最小的素数2,任意素数长度区间 L = 2k + 1, 因此只要任意长度为2和3的区间满足条件即可
假设 1 = 红, 0 = 蓝  
易得 F( 2 ) = 3,
F(3) = 4
要求长度为n的串,可以在n-1的串后加1 或 0,
加 1 肯定满足要求
加 0 要满足 n-1 串的末尾不能是0,且倒数第二位也不能是0,因此在n-3串后直接加110
得递推式 F(n) = F(n-1) + F(n-3) 
-------------------------------------------------------------------------
再用 矩阵快速幂 加速求递推式
 

 求得 A =

       

 

【AC代码】

 

/*
矩阵快速幂模板 
by chsobin
*/
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn = 3;
const ll mod = 1e9 + 7;

//矩阵结构体 
struct Matrix{
    ll a[maxn][maxn];
    void init(){    //初始化为单位矩阵 
        memset(a, 0, sizeof(a));
        for(int i=0;i<maxn;++i){
            a[i][i] = 1;
        }
    }
};

//矩阵乘法 
Matrix mul(Matrix a, Matrix b){
    Matrix ans;
    for(int i=0;i<maxn;++i){
        for(int j=0;j<maxn;++j){
            ans.a[i][j] = 0;
            for(int k=0;k<maxn;++k){
                ans.a[i][j] += a.a[i][k] * b.a[k][j];
                ans.a[i][j] %= mod; 
            }
        }
    } 
    return ans;
}

//矩阵快速幂 
Matrix qpow(Matrix a, ll n){
    Matrix ans;
    ans.init();
    while(n){
        if(n&1) ans = mul(ans, a);
        a = mul(a, a);
        n /= 2;
    } 
    return ans;
}

////for debug
//void output(Matrix a){
//    for(int i=0;i<maxn;++i){
//        for(int j=0;j<maxn;++j){
//            cout << a.a[i][j] << " ";
//        }
//        cout << endl;
//    }
//}


int main(){
    Matrix A = { 1,1,0,0,0,1,1,0,0 };    //递推矩阵 
    
    int t;
    cin >> t;
    ll n;
    int ans[5] = {0, 0, 3, 4, 6}; //枚举初始情况 
    while(t--){
        cin >> n;
        if(n<=4) cout << ans[n] << endl;
        else{
            Matrix m = qpow(A , n-4);
            ll temp = 6 * m.a[0][0] % mod;
            temp = (temp + 4 * m.a[1][0]%mod)%mod;
            temp = (temp + 3 * m.a[2][0]%mod)%mod;
            cout << temp << endl;
        }
    }
    return 0;
}
AC

 

【总结】:

         看到数据量就明白了这一类题的做法

         1.打表

         2.根据打表结果,写出递推方程。

         3.将递推方程构造为矩阵。

         4.快速幂求解。

 

 
 
 
 
 
 
 
 

转载于:https://www.cnblogs.com/chsobin/p/8417927.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值