HDU-6030-Happy Necklace

Happy Necklace

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1693    Accepted Submission(s): 715


 

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

 题目大意:一个手链只有红色和蓝色的珠子,假设红色是1,蓝色是0,在任意的素数个数的珠子中红色大于等于蓝色,分析可知若要满足条件只需要考虑长度为2和长度为3的串。

所以我们可以得到推导公式:f[n]=f[n-1]+f[n-3];

由于n的取值范围比较大,所以我们可以采用矩阵快速幂来做

推导出初始矩阵为:

                    然后全部加起来就可以了

所以代码:

#include<map>
#include<stack>
#include<cmath>
#include<queue>
#include<string>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
const int maxn=1010;
const int inf=0x3f3f3f;
const int mod=1e9+7;
const int N=3;
ll res[N][N];
void juzhen(ll a[N][N],ll b[N][N])
{
    ll c[N][N],i,j,k;
    memset(c,0,sizeof(c));
    for(k=0; k<N; k++)
    {
        for(i=0; i<N; i++)
        {
            for(j=0; j<N; j++)
            {
                c[k][i]+=a[k][j]*b[j][i];
                c[k][i]%=mod;
            }
        }
    }
    for(i=0; i<N; i++)
    {
        for(j=0; j<N; j++)
            a[i][j]=c[i][j];
    }
}
void ppow(ll a[N][N],ll n)
{
    memset(res,0,sizeof(res));
    for(int i=0; i<N; i++)
        res[i][i]=1;
    while(n)
    {
        if(n&1)
            juzhen(res,a);
        juzhen(a,a);
        n>>=1;
    }
    ll x = (res[0][0]+res[0][1]+res[0][2]) % mod;
    ll y = (res[1][0]+res[1][1]+res[1][2]) % mod;
    ll z = (res[2][0]+res[2][1]+res[2][2]) % mod;
    printf("%lld\n",(x + y + z)%mod);
}
int main()
{
    int test;
    scanf("%d",&test);
    while(test--)
    {
        ll n;
        scanf("%lld",&n);
        if(n==2)
        {
            printf("3\n");
            continue;
        }
        ll a[N][N];
        a[0][0] = 1; a[0][1] = 0; a[0][2] = 1;
		a[1][0] = 1; a[1][1] = 0; a[1][2] = 0;
		a[2][0] = 0; a[2][1] = 1; a[2][2] = 0;
        ppow(a,n-2);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值