传送门:点我

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

InputThe 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≤10^18), denoting the number of beads on the necklace.OutputFor each test case, print a single line containing a single integer, denoting the answer modulo 1E9+7.Sample Input

2
2
3

Sample Output

3
4 大意是:一个串(不结环)由红色和蓝色珠子组成,要求每素数个串红色的珠子数量大于等于蓝色的,给定串的长度,询问问能构成的串的种类数,mod 1e9+7。

思路:

红用A表示,蓝用B表示
显然当n==2:
有 AB,AA,BA三种情况
记a[n],b[n],c[n]分别为以三种为末尾的字符串的个数
当n==3时:
AB后面可以加A ==> ABA (末尾为BA,下同)
AA后面可以加 A或B ==> AAA,AAB
BA后面可以加A ==> BAA
得到递推式
a[n] = b[n-1]
b[n] = b[n-1]+c[n-1]
c[n] = a[n-1]
记题目所求的个数为p[n]
p[n] = a[n]+b[n]+c[n] = 2*b[n-1]+a[n-1]+c[n-1] = (a[n-1]+b[n-1]+c[n-1]) + b[n-1] = p[n-1] + (b[n-2]+c[n-2]) = p[n-1] + (b[n-3]+c[n-3]+a[n-3]) = p[n-1]+p[n-3]
到此得到递推关系式:p[n] = p[n-1]+p[n-3]
变成矩阵快速幂的模版题了。

  代码:

  

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <bitset>
#define LL long long
#define pb push_back
#define mk make_pair
#define pill pair<int, int>
#define mst(a, b) memset(a, b, sizeof a)
#define REP(i, x, n) for(int i = x; i <= n; ++i)
#define pi acos(-1.0)
#define Max_N 1001
#define inf 0x3f3f3f3f
#define N 1001
#define ll long long
using namespace std;
const LL MOD = 1e9+;
struct mat{
LL a[][];
};
mat mat_mul(mat x,mat y,int len){
mat res;
memset(res.a,,sizeof(res.a));
for(int i = ; i < len ; i ++){
for(int j = ; j < len ;j ++){
for(int k = ; k < len ;k ++)
res.a[i][j] = (res.a[i][j] + (x.a[i][k]*y.a[k][j]*1LL)%MOD)%MOD;
}
}
return res;
}
mat mat_qpow(mat a,LL b,int len){
mat ans;
memset(ans.a,,sizeof(ans.a));
for(int i = ;i < len ; i ++) ans.a[i][i] = 1LL;
while(b){
if(b&) ans = mat_mul(ans,a,len);
a = mat_mul(a,a,len);
b>>=;
}
return ans;
}
int main(){
int t;
scanf("%d",&t);
while(t--)
{
LL n;
scanf("%lld",&n);
if(n == ){
puts("");continue;
}
else if(n == ){
puts("");continue;
}
else if(n == ){
puts("");continue;
}
mat b;
b.a[][] = ; b.a[][] = ; b.a[][] = ;
b.a[][] = ; b.a[][] = ; b.a[][] = ;
b.a[][] = ; b.a[][] = ; b.a[][] = ;
b = mat_qpow(b,n-,);
LL ans = (b.a[][]*6LL+b.a[][]*4LL+b.a[][]*3LL)%MOD;
cout<<ans<<endl;
}
return ;
}
/*
2
3
4
*/

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

  1. HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  2. hdu 2604 递推 矩阵快速幂

    HDU 2604 Queuing (递推+矩阵快速幂) 这位作者讲的不错,可以看看他的 #include <cstdio> #include <iostream> #inclu ...

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

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

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

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

  5. Recursive sequence HDU - 5950 (递推 矩阵快速幂优化)

    题目链接 F[1] = a, F[2] = b, F[i] = 2 * F[i-2] + F[i-1] + i ^ 4, (i >= 3) 现在要求F[N] 类似于斐波那契数列的递推式子吧, 但 ...

  6. 五校联考R1 Day1T3 平面图planar(递推 矩阵快速幂)

    题目链接 我们可以把棱柱拆成有\(n\)条高的矩形,尝试递推. 在计算的过程中,第\(i\)列(\(i\neq n\))只与\(i-1\)列有关,称\(i-1\)列的上面/下面为左上/左下,第\(i\ ...

  7. LightOJ 1244 - Tiles 猜递推+矩阵快速幂

    http://www.lightoj.com/volume_showproblem.php?problem=1244 题意:给出六种积木,不能旋转,翻转,问填充2XN的格子有几种方法.\(N < ...

  8. [递推+矩阵快速幂]Codeforces 1117D - Magic Gems

    传送门:Educational Codeforces Round 60 – D   题意: 给定N,M(n <1e18,m <= 100) 一个magic gem可以分裂成M个普通的gem ...

  9. [hdu 2604] Queuing 递推 矩阵快速幂

    Problem Description Queues and Priority Queues are data structures which are known to most computer ...

随机推荐

  1. PythonStudy——比较运算符 Comparison operator

    1.运算结果为bool类型 print(3 > 5) Output: False 2.可以连比 num = 10 print(1 < num < 20)# 与之上的等价写法是: pr ...

  2. 【DB2】SQL1585N 由于没有具有兼容页面大小的可用系统临时表空间,因此无法创建临时表。SQLSTATE=54048

    自己写了一段SQL,SQL中包含ORDER BY 字句,但是在执行的时候报错如下: 经过查询发现是由于临时表空间的PAGESIZE不够大,可考虑建16k或者32k PAGESIZE的表空间 示例如下: ...

  3. DevExpress中barManager下的toolbar如何在panel中显示

    如题,我的Dev Toolbar需要在一个pannel中显示,并且居于最顶部.可是好像默认情况下toolbar都是在窗体的最顶部的,如何设置才能使其位于一个panel的最顶部呢? 解决方案:经过测试, ...

  4. Docker之数据卷Volume(七)

    一.简介   Docker数据卷(volume)机制.volume是存在于一个或多个容器中的特定文件或文件夹,这个目录以独立于联合文件系统的形式在宿主机中存在,并为数据的共享与持久化提供便利. 1)v ...

  5. linux tomcat jvm调优

    修改TOMCAT_HOME/bin/catalina.sh文件: # OS specific support.  $var _must_ be set to either true or false. ...

  6. Excel技巧--文件批处理

    先认识几个dos命令: ren 旧文件名 新文件名:更改文件名: dir 文件路径 /b > 目标路径/表名.xls:查询指定文件路径下的所有文件,写入到目标路径下的excel文件中: md 新 ...

  7. NetCore2.0 CodeFirst 解析全国区划信息

    NetCore2.0 数据库:SQLite HTML解析:HtmlAgilityPack 区划数据:http://www.stats.gov.cn/tjsj/tjbz/xzqhdm/201703/t2 ...

  8. C 栈实现队列节点的管理

    栈预先存储节点,队列的malloc/free均有栈模拟,从而保证不频繁的开辟/是否节点内存. #include "com_is_buf.h" #include "com_ ...

  9. git clone git@github.com:xxx.git Permission denied (publickey) 问题解决办法

    From: https://www.cnblogs.com/restart/p/4633928.html 如果git无法通过普通的http去clone远程分支,可以选用ssh方式去连接.这时需要配置相 ...

  10. centos7-网络连接

    Centos系统在安装完毕后,默认联网状态为no,需要手动开启联网状态. 编辑网卡文件 vim /etc/sysconfig/network-scripts/ifcfg-eno16777736 修改O ...