传送门:
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 nn 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 1E9+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≤10^18), denoting the number of beads on the necklace.OutputFor each test case, print a single line containing a single integer, denoting the answer modulo 1E9+7.Sample Input
223
Sample Output
34 大意是:一个串(不结环)由红色和蓝色珠子组成,要求每素数个串红色的珠子数量大于等于蓝色的,给定串的长度,询问问能构成的串的种类数,mod 1e9+7。
思路:
红用A表示,蓝用B表示
显然当n==2:有 AB,AA,BA三种情况记a[n],b[n],c[n]分别为以三种为末尾的字符串的个数当n==3时:AB后面可以加A ==> ABA (末尾为BA,下同)AA后面可以加 A或B ==> AAA,AABBA后面可以加A ==> BAA得到递推式 a[n] = b[n-1]b[n] = b[n-1]+c[n-1]c[n] = a[n-1]记题目所求的个数为p[n]p[n] = a[n]+b[n]+c[n] = 2*b[n-1]+a[n-1]+c[n-1] = (a[n-1]+b[n-1]+c[n-1]) + b[n-1] = p[n-1] + (b[n-2]+c[n-2]) = p[n-1] + (b[n-3]+c[n-3]+a[n-3]) = p[n-1]+p[n-3]到此得到递推关系式:p[n] = p[n-1]+p[n-3]变成矩阵快速幂的模版题了。代码:
#include#include #include #include #include #include #include #include #include #include #include #include #include