HDU6030 Happy Necklace(递推+矩阵快速幂)
传送门:点我
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(递推+矩阵快速幂)的更多相关文章
- HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- hdu 2604 递推 矩阵快速幂
HDU 2604 Queuing (递推+矩阵快速幂) 这位作者讲的不错,可以看看他的 #include <cstdio> #include <iostream> #inclu ...
- 2017中国大学生程序设计竞赛 - 女生专场 Happy Necklace(递推+矩阵快速幂)
Happy Necklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- HDU 2842 (递推+矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...
- 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] 类似于斐波那契数列的递推式子吧, 但 ...
- 五校联考R1 Day1T3 平面图planar(递推 矩阵快速幂)
题目链接 我们可以把棱柱拆成有\(n\)条高的矩形,尝试递推. 在计算的过程中,第\(i\)列(\(i\neq n\))只与\(i-1\)列有关,称\(i-1\)列的上面/下面为左上/左下,第\(i\ ...
- LightOJ 1244 - Tiles 猜递推+矩阵快速幂
http://www.lightoj.com/volume_showproblem.php?problem=1244 题意:给出六种积木,不能旋转,翻转,问填充2XN的格子有几种方法.\(N < ...
- [递推+矩阵快速幂]Codeforces 1117D - Magic Gems
传送门:Educational Codeforces Round 60 – D 题意: 给定N,M(n <1e18,m <= 100) 一个magic gem可以分裂成M个普通的gem ...
- [hdu 2604] Queuing 递推 矩阵快速幂
Problem Description Queues and Priority Queues are data structures which are known to most computer ...
随机推荐
- SELinux初探
- PythonStudy——元组 Tuple
元组类型 元组:可以理解为不可变的列表1.值可以为任意类型2.可以存放多个值 - 可以进行成员运算3.可以存放重复的值 - 可以计算成员出现的次数4.有序存储 - 可以通过索引取值,可以切片 常用操作 ...
- PythonStudy——PyCharm 选择性忽略PEP8代码风格警告信息
用了几天的PyCharm,发现确实在编写Python代码上非常好用,但有一点体验不太好,就是代码编写时要按照PEP8代码风格编写,不然会有波浪线的警告信息.解决方法如下: 方法一:将鼠标移到提示的地方 ...
- 【js字符串当做数组来使用】浪费一晚【想出了3个解决方案】
数据库的所有数据都打成字符串发到前端. 不必把它的类型也强制转成int这类的,页面负责字符串的展示 这样做可以修改页面的数据 response.setHeader("Content-type ...
- dmi-ipmi
api,cli,gui,tui,dmi(smbios),ipmi,bios,efi,uefi SMBIOS(System Management BIOS)是主板或系统制造者以标准格式显示产品管理信息所 ...
- 使用 vue-cli-service inspect 来查看一个 Vue CLI 3 项目的 webpack 配置信息(包括:development、production)
使用 vue-cli-service inspect 来查看一个 Vue CLI 3 项目的 webpack 配置信息(包括:development.production) --mode 指定环境模式 ...
- Flume调优
这是一个关于池子的故事.有一个池子,它一头进水,另一头出水,进水口可以配置各种管子,出水口也可以配置各种管子,可以有多个进水口.多个出水口.水术语称为Event,进水口术语称为Source.出水口术语 ...
- docker 进程监控 Dumb-Init进程信号处理 --转自https://blog.csdn.net/tiger435/article/details/54971929
随着docker及Kubernetes技术发展的越来越成熟稳定,越来越多的公司开始将docker用于生产环境的部署,相比起物理机上直接部署,多了一层docker容器的环境,这就带来一个问题:进程信号接 ...
- 数据访问安全--数据库遮罩及断词 Data Masking & Tokenization
现在大数据时代几乎无隐私,各政府部门各公司都要求实名制(动不动手机认证,身份证号码认证),但又无力确保数据安全,称为乱象. 其实在2011年,我们就接触过数据库遮罩断词产品,一个澳大利亚公司产品. 简 ...
- 02-模拟Junit4功能
package com.day2; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; im ...