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

题意: 给出红蓝两种,然后排成一个字符串,要求在每一个长度为素数的区间里面是的r(red)的数量不小与b(blue)的数量;

思路:想象当n为2的时候的情况是 rr,rb,br,三种情况,当n为3的时候相当于在后面添加一个b或者r,会发现形成rr的情况是前面rr和br的和,形成br的情况是前面的rb,而形成rb的情况是前面的rr,不能有前面的br形成rb,因为在素数为3的时候不能形成brb;

所以你会发现这个针对的素数只是2和3;

根据递推,设数组a[],b[],c[]分别为后面两个字母为rr,br,rb的字符串的数量,那么可以得到递推式:

a[i] = a[i - 1] + c[i - 1];b[i] = a[i - 1];c[i] = b[i - 1];

而题中要求的是所有的字符串,即s[n] = a[n] + b[n] + c[n];

可以得出s[i] = s[i - 1] + s[i - 3];

n的范围是10^18,那么只能用到矩阵快速幂: 可以推出最初的矩阵为

1 0 1
1 0 0
0 1 0
#include <bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
typedef long long ll;
struct martix{
ll mo[4][4];
martix(){
memset(mo,0,sizeof(mo));
}
};
martix mul(martix a,martix b){
martix c;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
for(int k=0;k<4;k++){
c.mo[i][j]=(c.mo[i][j]+a.mo[i][k]*b.mo[k][j])%mod;
}
}
}
return c;
}
ll powmod(martix a,ll n){
martix T;
for(int i=0;i<4;i++){
T.mo[i][i]=1;
}
while(n){
if(n&1) T=mul(a,T);
n>>=1;
a=mul(a,a);
}
return (T.mo[0][0]*6+T.mo[0][1]*4+T.mo[0][2]*3)%mod;
}
int main()
{
ll t;
cin>>t;
while(t--){
ll n;
cin>>n;
int ans[5]={0,0,3,4,6};
if(n<=4){
cout<<ans[n]%mod<<endl;
}
else{
martix q;
q.mo[0][0]=q.mo[0][2]=q.mo[3][2]=q.mo[1][0]=q.mo[2][1]=q.mo[3][2]=1;
cout<<powmod(q,n-4)%mod<<endl;
}
}
return 0;
}

矩阵快速幂--HDU 6030 Happy Necklace的更多相关文章

  1. HDU 6395 分段矩阵快速幂 HDU 6386 建虚点+dij

    http://acm.hdu.edu.cn/showproblem.php?pid=6395 Sequence Time Limit: 4000/2000 MS (Java/Others)    Me ...

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

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

  3. 循环节 + 矩阵快速幂 - HDU 4291 A Short problem

    A Short problem Problem's Link Mean: 给定一个n,求:g(g(g(n))) % 1000000007 其中:g(n) = 3g(n - 1) + g(n - 2), ...

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

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

  5. HDU 2855 斐波那契+矩阵快速幂

    http://acm.hdu.edu.cn/showproblem.php?pid=2855 化简这个公式,多写出几组就会发现规律 d[n]=F[2*n] 后面的任务就是矩阵快速幂拍一个斐波那契模板出 ...

  6. HDU 2855 (矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2855 题目大意:求$S(n)=\sum_{k=0}^{n}C_{n}^{k}Fibonacci(k)$ ...

  7. HDU 2842 (递推+矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...

  8. HDU 4471 矩阵快速幂 Homework

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4471 解题思路,矩阵快速幂····特殊点特殊处理····· 令h为计算某个数最多须知前h个数,于是写 ...

  9. HDU 5950:Recursive sequence(矩阵快速幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:给出 a,b,n,递推出 f(n) = f(n-1) + f(n-2) * 2 + n ^ 4. f ...

随机推荐

  1. js定义类的方法

    ECMAScript6已经支持了class,但之前版本都不支持类,但是可以通过一些方法来模拟类. js中的类,既是重点,也是难点,很多时候都感觉模棱两可. 首先强调一下js中很重要的3个知识点:thi ...

  2. protobuf总结

    1.protobuf是什么? protobuf(protocol buffers)是一种语言中立,平台无关,可扩展的序列化数据的格式,可以用于通信协议,数据存储等. protobuf 相比于xml,j ...

  3. 五个简单的shell脚本

    1.编写shell脚本 ex1.sh,提示用户输入用户名,并判断此用户名是否存在. (提示:利用read.grep和/etc/passwd) #!/bin/bash echo "请输入用户名 ...

  4. Spire.Cloud 私有化部署教程(一) - CentOS 7 系统

    Spire.Cloud支持的Linux服务器系统包括CentOS和Ubuntu(推荐使用CentOS 7和Ubuntu 18版本),本教程主要介绍如何在CentOS 7系统上实现Spire.Cloud ...

  5. 在java 中一种简单方式的声明静态Map常量的方法

    我现在需要在一个类里面放一个HashMap,往里面放一些数据,每次要从数据库中取数据的时候先查找HashMap,看是否已经存在,若存在就直接提取,若不存在就从数据库中抽取数据之后再放到HashMap中 ...

  6. 谁说.NET不适合搞大数据,机器学习、人工智能

    SciSharp Stack SciSharp STACK: https://scisharp.github.io/SciSharp/ 基于.NET的开源生态系统,用于数据科学.机器学习和AI. Sc ...

  7. 知识点一:OSI模型初识

    OSI(开放系统)模型是一组协议的集合,它使得两个不同的系统之间能够互相通信,分为七层 第一层:物理层 物理层负责把逐个的比特(01)从一个节点移动到下个节点 具体体现在如何把比特转换成电或者光信号. ...

  8. L14梯度消失、梯度爆炸

    梯度消失.梯度爆炸以及Kaggle房价预测 梯度消失和梯度爆炸 考虑到环境因素的其他问题 Kaggle房价预测 梯度消失和梯度爆炸 深度模型有关数值稳定性的典型问题是消失(vanishing)和爆炸( ...

  9. The Super Powers UVA - 11752

    题目大意:将范围从1~pow(2,64)-1内的super power输出.super power的定义:一个数x至少存在两种x=pow(i,k),(k!=1). 题解: 注意数据范围2的64次方-1 ...

  10. 使用jquery清空input 文本框中的内容

    只需要将文本框的值置为空即可: function resetBtn(){ $("#name").val(""); }