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

Happy Necklace

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


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 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 .
Note: The necklace is a single string, {not a circle}.
 


Input
The first line of the input contains an integer , denoting the number of test cases.
For each test case, there is a single line containing an integer , 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 .
 


Sample Input
 
  
223
 

 

Sample Output
 
  
34
 

 

Source
题目描述:你有一个有n个颜色为红色或蓝色的珠子的项链,你可以将项链截断成长度为素数的珠子,如果截取出的一段红色的珠子的个数大于蓝色的珠子,则成为good,问一共有多少种good的可能性。
    可以发现
    n=2,res=3;n=3,res=4;n=4,res=6;n=5,res=9;
    故有递推式 An=An-1+An-3;

    如果用a表示红色,用b表示蓝色。题意明显可以看出只需要管长度2和3的连续序列是否符合!

    如果以b结尾,那么下一个必须是a,或者加个aab就可以了!

 

    所以同理就可以推出递推式An=An-1+An-3;
    看一下题目的数据范围,n最大1e18,因此常规O(n)的递推显然不可行,因此直接上O(logn)的矩阵快速幂。

 

 

    发现递推式是四阶的递推式,故所得的常数矩阵应该是四维的。之后只需带入矩阵快速幂模板即可。

先看这个特征方程F[i] = F[i - 1] + F[i - 3],那么就有一个矩阵如下

我们的目标矩阵就是

那么,针对这个矩阵我们如何转置呢?

先看目标矩阵第一个:F[i]

F[i] = F[i - 1] + F[i - 3]

那么,由矩阵乘法,转置矩阵第一行,似乎就定了:1 0 1

同样的,二三行就是1 0 0 和 0 1 0

整个矩阵如下:

 

#include<iostream>  
#include<algorithm>  
#include<cmath>  
#include<cstdio>  
#include<cstring>  
  
#define INF 0x3f3f3f3f  
  
#define mod 1000000007  
  
using namespace std;  
  
typedef long long ll;  
const int maxn = 100010;  
  
ll n;  
  
struct Matrix {  
    ll a[5][5];  
};  
  
  
Matrix mul(Matrix x, Matrix y)  
{  
    Matrix temp;  
    for (int i = 1; i <= 3; i++)  
        for (int j = 1; j <= 3; j++) temp.a[i][j] = 0;  
  
    for (int i = 1; i <= 3; i++)  
    {  
        for (int j = 1; j <= 3; j++)  
        {  
            ll sum = 0;  
            for (int k = 1; k <= 3; k++)  
            {  
                sum = (sum + x.a[i][k] * y.a[k][j] % mod) % mod;  
            }  
            temp.a[i][j] = sum;  
        }  
    }  
    return temp;  
}  
  
Matrix quickpow(Matrix A,ll k)  
{  
    Matrix res;  
    res.a[1][1] = 1; res.a[1][2] = 0; res.a[1][3] = 0;  
    res.a[2][1] = 0; res.a[2][2] = 1; res.a[2][3] = 0;  
    res.a[3][1] = 0; res.a[3][2] = 0; res.a[3][3] = 1;  
    while (k)  
    {  
        if (k & 1) res = mul(res, A);  
        A = mul(A, A);  
        k >>= 1;  
    }  
    return res;  
}  
  
int main()  
{  
    int t;  
    scanf("%d", &t);  
    while (t--)  
    {  
        scanf("%lld", &n);  
        if (n == 2)  
        {  
            printf("3\n");  
            continue;  
        }  
        Matrix A;  
        A.a[1][1] = 1; A.a[1][2] = 0; A.a[1][3] = 1;  
        A.a[2][1] = 1; A.a[2][2] = 0; A.a[2][3] = 0;  
        A.a[3][1] = 0; A.a[3][2] = 1; A.a[3][3] = 0;  
        Matrix res = quickpow(A, n - 2);  
        ll x = (res.a[1][1] + res.a[1][2] + res.a[1][3]) % mod;  
        ll y = (res.a[2][1] + res.a[2][2] + res.a[2][3]) % mod;  
        ll z = (res.a[3][1] + res.a[3][2] + res.a[3][3]) % mod;  
        printf("%lld\n", (x + y + z) % mod);  
    }  
}  

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学群体。无论你是计算机相关专业的学,还是对其他领域编程感兴趣的学,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蔡军帅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值