標籤:lease style compose size acm script tle class his
題目連結 :http://acm.hdu.edu.cn/showproblem.php?pid=6030
Problem DescriptionLittle 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}. InputThe 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. OutputFor each test case, print a single line containing a single integer, denoting the answer modulo 109+7. Sample Input223 Sample Output34 Source2017中國大學生程式設計競賽 - 女生專場
題目大意:有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 1000000007struct 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;
}
(hdu 6030) Happy Necklace 找規律+矩陣快速冪