(hdu 6030) Happy Necklace 找规律+矩阵快速幂

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(hdu 6030) Happy Necklace 找规律+矩阵快速幂相关的知识,希望对你有一定的参考价值。

题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=6030

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
2017中国大学生程序设计竞赛 - 女生专场

题目大意:有n个珠子,有红蓝两种珠子 符合下列两个条件的珠子排列方法有多少种?

*******在每串手链种,长度为素数的子串种,红色珠子要不小于蓝色珠子

分析:找到规律:f[i] = f[i-1]+f[i-3];因为n很大   可以用矩阵快速幂写

初始矩阵为2  3  4  转移矩阵为0  0  1

                                               1   0  0

                                               0   1  1

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include <vector>
#include<iostream>
using namespace std;
#define N 50005
#define INF 0x3f3f3f3f
#define LL long long
#define mod 1000000007
struct node
{
    LL a[10][10];
};
node mul(node a,node b)
{
    node te;
    memset(te.a,0,sizeof(te.a));
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            for(int k=0;k<3;k++)
            {
                te.a[i][j]+=(a.a[i][k]*b.a[k][j])%mod;
            }
        }
    }
    return te;
}
LL quick(LL n)
{
    if(n<4)
    {
        int num[]={0,2,3,4};
        return num[n];
    }
    node b,team;
    b.a[0][0] = 0;b.a[0][1] = 0;b.a[0][2] = 1;
    b.a[1][0] = 1;b.a[1][1] = b.a[1][2] = 0;
    b.a[2][0] = 0;b.a[2][1] = b.a[2][2] = 1;
    memset(team.a,0,sizeof(team.a));
    for(int i=0;i<3;i++)
        team.a[i][i] = 1;
    LL k = n-3;
    while(k)
    {
        if(k&1)
        team = mul(team,b);
        b = mul(b,b);
        k = k/2;
    }
    return (team.a[0][2]*2+team.a[1][2]*3+team.a[2][2]*4)%mod;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        LL n;
        scanf("%lld",&n);
        printf("%lld\n",quick(n));
    }
    return 0;
}

 



以上是关于(hdu 6030) Happy Necklace 找规律+矩阵快速幂的主要内容,如果未能解决你的问题,请参考以下文章

HDU 6030 Happy Necklace

HDU6030 Happy Necklace(推导+矩阵快速幂)

Happy Necklace (矩阵快速幂 + 递推 + 取模)

2017中国大学生程序设计竞赛 - 女生专场 Happy Necklace(递推+矩阵快速幂)

hdu 6030

杭电女生赛1001 1002 1003 1005 1008 hdu6023 6024 6025 6027 6030