2017 CCPC-WFinal&&HDOJ 6030 Happy Necklace(矩阵快速幂)

Happy Necklace

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

Problem Description

Little Q wants to buy anecklace for his girlfriend. Necklaces are single strings composed of multiplered and blue beads.
Little Q desperately wants to impress his girlfriend, he knows that she willlike the necklace only if for every prime length continuous subsequence in thenecklace, 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 thenumber of different necklaces that can make his girlfriend happy. Please writea program to help Little Q. Since the answer may be very large, please printthe answer modulo 109+7.
Note: The necklace is a single string,
{not a circle}.

 

 

Input

The first line of the inputcontains an integerT(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 asingle line containing a single integer, denoting the answer modulo109+7

 

 

Sample Input

2

2

3

 

 

Sample Output

3

4


乍一看很难的样子orz。。。但其实只要推出前几组就可以找到规律(但为什么是这样其实我也不知道QAQ)

当i<4 时,a[i]=i+1;

当i>=4时,a[i]=a[i-2]+a[i-3]+a[i-4];

由此可以想到用矩阵快速幂做,直接套模板即可

  | 0 1 1 1 |     | F[n-1] |     |   F[n]   |

  | 1 0 0 0 |  *  | F[n-2] | = | F[n-1] |

  | 0 1 0 0 |     | F[n-3] |    | F[n-2] |

  | 0 0 1 0 |     | F[n-4] |    | F[n-3] |


#include <bits/stdc++.h>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define f(i,a,b) for(int i=(a);i<(b);++i)
#define ll long long
const int maxn = 4;
const int mod = 1e9+7;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
#define rush() int T;scanf("%d",&T);while(T--)
struct Matrix
{
    ll temp[maxn][maxn];
} a;
void init()
{
    f(i,0,maxn)
    f(j,0,maxn)
    {
        a.temp[i][j]=0;
    }
    a.temp[0][1]=a.temp[0][2]=a.temp[0][3]=1;
    a.temp[1][0]=a.temp[2][1]=a.temp[3][2]=1;
}
Matrix mul(Matrix a,Matrix b)
{
    Matrix ans;
    for (int i=0; i<maxn; i++)
        for (int j=0; j<maxn; j++)
        {
            ans.temp[i][j]=0;
            for (int k=0; k<maxn; k++)
            {
                ans.temp[i][j]+=a.temp[i][k]*b.temp[k][j];
                ans.temp[i][j]%=mod;
            }
        }
    return ans;
}
void fun(Matrix ans,ll k)
{
    for(int i=0; i<maxn; i++)
        for(int j=0; j<maxn; j++)
            a.temp[i][j]=(i==j);
    while(k)
    {
        if(k%2)
            a=mul(a,ans);
        ans=mul(ans,ans);
        k/=2;
    }
}
int main()
{
    Matrix t;
    ll n;
    f(i,0,maxn)
    f(j,0,maxn)
    {
        t.temp[i][j]=0;
    }
    t.temp[0][0]=4;
    t.temp[1][0]=3;
    t.temp[2][0]=2;
    t.temp[3][0]=1;
    rush()
    {
        init();
        scanf("%I64d",&n);
        if(n<4)
        {
            printf("%I64d\n",n+1);
            continue;
        }
        fun(a,n-3);
        a=mul(a,t);
        ll ans=a.temp[0][0]%mod;
        printf("%I64d\n",ans);
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值