hdu6030 矩阵快速幂

时间:2019-11-27
本文章向大家介绍hdu6030 矩阵快速幂,主要包括hdu6030 矩阵快速幂使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Happy Necklace

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
题意: 每个素数区间的红色个数大于蓝色  (0,1 都不是素数)
    F(n)=F(n-1)+F(n-3);
 
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=3;  //矩阵大小 
const int mod=1e9+7;
struct Matrix{
    ll m[maxn][maxn];  
    Matrix(){
        memset(m,0,sizeof(m));
    }
};
Matrix Multi(Matrix a,Matrix b){
    Matrix res;
    for(int i=0;i<maxn;i++){
        for(int j=0;j<maxn;j++){
            for(int k=0;k<maxn;k++){
                res.m[i][j]=(res.m[i][j]+ (a.m[i][k]%mod) *( b.m[k][j]%mod)%mod ) %mod;
            } 
        }
    }
    return res;
} 
Matrix fastm(Matrix a,ll num){
    Matrix res;
    res.m[0][0]=6;
    res.m[1][0]=4;
    res.m[2][0]=3;
    while(num){
        if(num&1){
            res=Multi(a,res);
        }
        a=Multi(a,a);
        num>>=1;
    }
    return res;
}
int ans[3]={3,4,6};
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        ll n;
        scanf("%lld",&n);
        if(n<=4){
            printf("%d\n",ans[n-2]);
        }else{
            Matrix a;
            a.m[0][0]=1;
            a.m[0][1]=0;
            a.m[0][2]=1;
            a.m[1][0]=1;
            a.m[1][1]=0;
            a.m[1][2]=0;
            a.m[2][0]=0;
            a.m[2][1]=1;
            a.m[2][2]=0;
            a=fastm(a,n-4);
            printf("%lld\n",a.m[0][0]);
        }
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/lyj1/p/11945505.html