Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
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 10^9+7. Note: The necklace is a single string, {not a circle}.
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≤10^18), denoting the number of beads on the necklace.
For each test case, print a single line containing a single integer, denoting the answer modulo 10^9+7.
题意: 一串项链(非环),上有蓝红两色宝石,若蓝色之间不能紧挨着且蓝色之间不能只隔着一个红色,求一共的方法。
题解: 设数组a[],b[],c[]分别为后面两个字母为rr,br,rb的字符串的数量。可以得到: a [ i ] = a [ i − 1 ] + c [ i − 1 ] b [ i ] = a [ i − 1 ] c [ i ] = b [ i − 1 ] \begin{aligned}a[i] &= a[i-1]+c[i-1]\\ b[i] &= a[i-1]\\ c[i] &= b[i-1]\end{aligned} a[i]b[i]c[i]=a[i−1]+c[i−1]=a[i−1]=b[i−1]
因而有: s [ n ] = a [ n ] + b [ n ] + c [ n ] = 2 ∗ a [ i − 1 ] + c [ i − 1 ] + b [ i − 1 ] = s [ n − 1 ] + a [ i − 1 ] = s [ n − 1 ] + a [ i − 2 ] + c [ i − 2 ] = s [ n − 1 ] + a [ i − 3 ] + b [ i − 3 ] + c [ i − 3 ] = s [ n − 1 ] + s [ n − 3 ] \begin{aligned}s[n]&=a[n]+b[n]+c[n]\\ &=2*a[i-1]+c[i-1]+b[i-1]\\ &=s[n-1]+a[i-1]\\ &=s[n-1]+a[i-2]+c[i-2]\\ &=s[n-1]+a[i-3]+b[i-3]+c[i-3]\\ &=s[n-1]+s[n-3]\end{aligned} s[n]=a[n]+b[n]+c[n]=2∗a[i−1]+c[i−1]+b[i−1]=s[n−1]+a[i−1]=s[n−1]+a[i−2]+c[i−2]=s[n−1]+a[i−3]+b[i−3]+c[i−3]=s[n−1]+s[n−3]
得到递归式: s [ n ] = s [ n − 1 ] + s [ n − 3 ] s[n]=s[n-1]+s[n-3] s[n]=s[n−1]+s[n−3] 然后即可用矩阵快速幂求解。