Happy Necklace
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2179 Accepted Submission(s): 901
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 Input
2 2 3
Sample Output
3 4
Source
Recommend
jiangzijing2015
题意:一串项链,在质数长度的任意区间内,红色的珠子不少于蓝色的珠子,问总共有多少种可能。
思路:简单分析一下,就是长度为2的串和长度为3的串要满足题意,我们做题的时候,一开始发现:
n=2 a=3
n=3 a=4
n=4 a=6
n=5 a=9
n=6 a=13
就很开心地以为规律是数列差成等差数列,然后又试了下n=7 a=19, 啊,不成立了
n=2 a=3
n=3 a=4
n=4 a=6
n=5 a=9
n=6 a=13
n=7 a=19
n=8 a=28
然后jd一拍草稿纸
仔细一想是这么个理。
考虑长度为6的时候,在长度为5的串前加上个0,肯定合法,方案数为
若在长度为5的串前加1,那么前三位就固定了,必须是100,后面就随意了,方案数为
找到规律了之后我们尝试推通项公式,失败。然后队长想到矩阵快速幂
这个是我构造的系数矩阵,然鹅,到n=6之后好像答案都会少1(挠头,没有想明白为什么)
然后队友改成
两个队友同步改,一个A了,一个T了。喵喵喵
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
struct Mat
{
ll m[5][5];
};
Mat mul(Mat a, Mat b)
{
Mat c;
memset(c.m, 0, sizeof(c.m));
for(int i = 1; i <= 4; i++)
{
for(int j = 1; j <= 4; j++)
{
for(int k = 1; k <= 4; k++)
{
c.m[i][j] = (c.m[i][j] + (a.m[i][k] * b.m[k][j])) % MOD;
}
}
}
return c;
}
void calc(Mat ori, ll n, Mat &res)
{
//Mat res;
memset(res.m, 0, sizeof(res.m));
res.m[1][1] = res.m[2][2] = res.m[3][3] = res.m[4][4] = 1; //初始化为单位矩阵
//res.m[1][1] = res.m[1][3] = res.m[2][2] = res.m[2][4] = res.m[3][2] = res.m[4][3] = 1;
//n--;
while(n)
{
if(n & 1)
{
res = mul(res, ori);
}
ori = mul(ori, ori);
n >>= 1;
}
}
int main()
{
int T;
cin >> T;
ll n;
while(T--)
{
scanf("%lld", &n);
if(n == 1)
printf("1\n");
else if(n == 2)
printf("3\n");
else if(n == 3)
printf("4\n");
else if(n == 4)
printf("6\n");
else
{
Mat ans;
Mat mid;
memset(mid.m, 0, sizeof(mid.m));
mid.m[1][1] = mid.m[1][3] = mid.m[2][1] = mid.m[3][2] = mid.m[4][3] = 1;
calc(mid, n - 4, ans);
ll sum = (ans.m[1][1] * 6 + ans.m[1][2] * 4 + ans.m[1][3] * 3 + ans.m[1][4] * 1) % MOD;
printf("%lld\n", sum);
}
}
return 0;
}