(hdu 6030) Happy Necklace找规律+矩阵快速幂

【(hdu 6030) Happy Necklace找规律+矩阵快速幂】天下之事常成于困约,而败于奢靡。这篇文章主要讲述(hdu 6030) Happy Necklace找规律+矩阵快速幂相关的知识,希望能为你提供帮助。
题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=6030

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(1≤T≤10000), denoting the number of test cases. For each test case, there is a single line containing an integer n(2≤n≤1018), 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 Input2 2 3 Sample Output3 4 Source 2017中国大学生程序设计竞赛 - 女生专场

题目大意:有n个珠子,有红蓝两种珠子 符合下列两个条件的珠子排列方法有多少种?
*******在每串手链种,长度为素数的子串种,红色珠子要不小于蓝色珠子
分析:找到规律:f[i] = f[i-1]+f[i-3]; 因为n很大    可以用矩阵快速幂写
初始矩阵为2  3  4  转移矩阵为0  0  1
                                                                                            1   0  0
                                                                                            0    1  1
#include< cstdio> #include< cstring> #include< cstdlib> #include< algorithm> #include< math.h> #include< queue> #include< stack> #include < vector> #include< iostream> using namespace std; #define N 50005 #define INF 0x3f3f3f3f #define LL long long #define mod 1000000007 struct node { LL a[10][10]; }; node mul(node a,node b) { node te; memset(te.a,0,sizeof(te.a)); for(int i=0; i< 3; i++) { for(int j=0; j< 3; j++) { for(int k=0; k< 3; k++) { te.a[i][j]+=(a.a[i][k]*b.a[k][j])%mod; } } } return te; } LL quick(LL n) { if(n< 4) { int num[]={0,2,3,4}; return num[n]; } node b,team; b.a[0][0] = 0; b.a[0][1] = 0; b.a[0][2] = 1; b.a[1][0] = 1; b.a[1][1] = b.a[1][2] = 0; b.a[2][0] = 0; b.a[2][1] = b.a[2][2] = 1; memset(team.a,0,sizeof(team.a)); for(int i=0; i< 3; i++) team.a[i][i] = 1; LL k = n-3; while(k) { if(k& 1) team = mul(team,b); b = mul(b,b); k = k/2; } return (team.a[0][2]*2+team.a[1][2]*3+team.a[2][2]*4)%mod; } int main() { int T; scanf("%d",& T); while(T--) { LL n; scanf("%lld",& n); printf("%lld\n",quick(n)); } return 0;
}

 



    推荐阅读