【解题报告】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


php

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 =ios

       

 

【AC代码】app

 

/*
矩阵快速幂模板 
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

 

【总结】:less

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

         1.打表atom

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

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

         4.快速幂求解。设计

相关文章
相关标签/搜索