传送门:点我

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. bash的工作特性(2)

    bash的工作特性之命令执行状态返回值和命令行展开所涉及的内容及其示例演示. 命令的执行结果状态 成功,失败 bash使用特殊变量$?保存最近一条命令的执行状态结果:echo$? 0:成功 1-255 ...

  2. pom.xml中添加远程仓库

    maven的pom.xml中添加远程仓库 <repositories> <repository> <id>mvnrepository</id> < ...

  3. 1_2_3_4_5 Html-Css

    --------------------------------------------------------------------------------- HTML骨架 <HTML> ...

  4. PythonStudy——列表的常用操作 List of common operations

    # 1.索引取值: 列表名[index] s1 = [1, 3, 2] print(s1[0]) print(s1[-1]) # 2.列表运算: 得到的是新list s2 = [1, 2, 3] pr ...

  5. alias重命名命令

    升级了openssh后,发现ctrl+l忽然无法清屏了. 如果需要清屏的话,就得执行clear,但我更喜欢简单粗暴的做法,于是想起alias命令 方式一: 如果只想在当前终端生效(exit该窗口终端后 ...

  6. golang: 利用unsafe操作未导出变量

    unsafe.Pointer其实就是类似C的void *,在golang中是用于各种指针相互转换的桥梁.uintptr是golang的内置类型,是能存储指针的整型,uintptr的底层类型是int,它 ...

  7. debian系统下apache2开启ssi功能

    SSI (Server Side Include)的 html 文件扩展名 (.shtml), 通常称为"服务器端嵌入"或者叫"服务器端包含"说白了就是类似其他 ...

  8. Excel函数之sumifs应用

    这个函数是用来进行多条件求和的一个函数 示例:根据表格求出“鼎盛书店”2012年度所有图书小计的总和,这里就用sumifs计算 首先定位结果输出单元格,祭出函数. sum_range参数就是筛选后需要 ...

  9. react使用ant-design组件库

    新建项目并引入组件 1,全局安装脚手架 npm install -g create-react-app 2,新建项目 create-react-app reactantd 3,安装组件 npm ins ...

  10. 用EXCEL做快速傅立葉轉換_FFT in Excel

    转载来自:http://yufan-fansbook.blogspot.tw/2013/09/excel-fft-fast-fourier-transform02.html [Excel]-用EXCE ...