Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 222    Accepted Submission(s): 91

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

//题意:给出红蓝两种珠子,要做一条长为 n 个珠子的项链,要求对于每一个素数长度的项链,都要使红色大于等于蓝色,问有多少种搭配方法

找出规律后,就知道只是简单的矩阵快速幂了,f[n] = f[n-1] + f[n-3]

 #include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define LL long long
#define MOD 1000000007
struct Mat
{
LL m[][];
}unit,base;
LL n; Mat Mult(Mat a,Mat b)
{
Mat re;
for (int i=;i<;i++)
{
for (int j=;j<;j++)
{
re.m[i][j]=;
for (int k=;k<;k++)
re.m[i][j]=(re.m[i][j]+ a.m[i][k]*b.m[k][j])%MOD;
}
}
return re;
} void Init()
{
base.m[][]=,base.m[][]=,base.m[][]=;
base.m[][]=,base.m[][]=,base.m[][]=;
base.m[][]=,base.m[][]=,base.m[][]=;
memset(unit.m,,sizeof(unit.m));
for (int i=;i<;i++) unit.m[i][i]=;
} LL cal(LL n)
{
if (n<) //n太小的情况
{
LL num[]={,,,};
return num[n];
}
Mat s=unit,b=base;
LL x = n-;
while(x)
{
if (x&) s = Mult(s,b);
b = Mult(b,b);
x/=;
}
return (s.m[][]*+s.m[][]*+s.m[][]*)%MOD;
} int main()
{
int T;
scanf("%d",&T);
while (T--)
{
scanf("%lld",&n);
Init();
printf("%lld\n",cal(n));
}
return ;
}

Happy Necklace(矩阵快速幂)的更多相关文章

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

    题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=6030 Problem Description Little Q wants to buy a nec ...

  2. HDU6030 Happy Necklace(递推+矩阵快速幂)

    传送门:点我 Little Q wants to buy a necklace for his girlfriend. Necklaces are single strings composed of ...

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

    Happy Necklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  4. 矩阵快速幂--HDU 6030 Happy Necklace

    Problem Description Little Q wants to buy a necklace for his girlfriend. Necklaces are single string ...

  5. HDU-6030 Happy Necklace 打表+矩阵快速幂

    Happy Necklace 前天个人赛规律都找出来了,n的范围是\(10^{18}\),我一想GG,肯定是矩阵快速幂,然后就放弃了. 昨天学了一下矩阵快速幂. 题意 现在小Q要为他的女朋友一个有n个 ...

  6. 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

    题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...

  7. 51nod 算法马拉松18 B 非010串 矩阵快速幂

    非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...

  8. 51nod 1113 矩阵快速幂

    题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...

  9. 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

    还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...

随机推荐

  1. What most young programmers need to learn

    In the past 7.5 years I have supervised over a dozen programming interns at Ronimo and have seen hun ...

  2. 【Python3 爬虫】16_抓取腾讯视频评论内容

    上一节我们已经知道如何使用Fiddler进行抓包分析,那么接下来我们开始完成一个简单的小例子 抓取腾讯视频的评论内容 首先我们打开腾讯视频的官网https://v.qq.com/ 我们打开[电视剧]这 ...

  3. xcode7.1.1不能真机调试ios9.2系统设备的解决方法

    转载自:http://www.cocoachina.com/bbs/read.php?tid-331335.html 前些天手机升级到iOS9.2版本号  xcode7.1还能真机測试. 昨晚更新xc ...

  4. php中的正则表达式

    一.定位字符 定位字符是正则表达式中又一类非常重要的字符,它的主要作用是用于对字符在匹配对象中的位置进行描述. ^:表示匹配的模式出现在匹配对象的开头(和在"[]"里面不同) $: ...

  5. SpringCloud系列十三:Feign对继承、压缩、日志的支持以及构造多参数请求

    1. 回顾 上文讲解了手动创建Feign,比默认的使用更加灵活. 本文将讲解Feign对继承.压缩的支持以及日志和多参数请求的构造等. 2. Feign对继承的支持 Feign支持继承.使用继承,可将 ...

  6. Atitit.编程语言的基础句型and汉语英文比较

    Atitit.编程语言的基础句型and汉语英文比较 1. 编程语言常见的句型1 1.1. Verb(obj)1 1.2. If  then else1 1.3. For1 2. 英语中的五种基本句型结 ...

  7. Python归并排序(递归实现)

    为什么归并排序如此有用?1. 快捷和稳定归并排序成为⼀一个非常棒的排序算法主要是因为它的快捷和稳定.它的复杂度即使在最差情况下都是O(n log n).而快速排序在最差情况下的复杂度是O(n^2),当 ...

  8. SQL数据库规范

    三范式 参考网址: http://www.cnblogs.com/linjiqin/archive/2012/04/01/2428695.html (1).第一范式(确保每列保持原子性) 每一列在某个 ...

  9. nginx 中location和root,你确定真的明白他们关系?

    最近公司开发新项目,web server使用nginx,趁周末小小的研究了一下,一不小心踩了个坑吧,一直404 not found!!!!!当时卡在location和root中,但是网上却比较少聊这方 ...

  10. 百度UEditor -- ZeroClipboard is not defined

    项目中一直在使用UEditor,风平浪静. 近期该项目引入了Requirejs,结果发现在有富文本编辑器的页面都会在控制台报出如下异常: Uncaught ReferenceError: ZeroCl ...