Happy Necklace(矩阵快速幂)
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
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(矩阵快速幂)的更多相关文章
- (hdu 6030) Happy Necklace 找规律+矩阵快速幂
题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=6030 Problem Description Little Q wants to buy a nec ...
- HDU6030 Happy Necklace(递推+矩阵快速幂)
传送门:点我 Little Q wants to buy a necklace for his girlfriend. Necklaces are single strings composed of ...
- 2017中国大学生程序设计竞赛 - 女生专场 Happy Necklace(递推+矩阵快速幂)
Happy Necklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- 矩阵快速幂--HDU 6030 Happy Necklace
Problem Description Little Q wants to buy a necklace for his girlfriend. Necklaces are single string ...
- HDU-6030 Happy Necklace 打表+矩阵快速幂
Happy Necklace 前天个人赛规律都找出来了,n的范围是\(10^{18}\),我一想GG,肯定是矩阵快速幂,然后就放弃了. 昨天学了一下矩阵快速幂. 题意 现在小Q要为他的女朋友一个有n个 ...
- 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)
题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...
- 51nod 算法马拉松18 B 非010串 矩阵快速幂
非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...
- 51nod 1113 矩阵快速幂
题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...
- 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】
还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...
随机推荐
- 初次玩耍lucene.net,一个小小的记录
lucene.net虽说是强大,但是我还是一年前第一次玩耍,然后就没有然后了,最近准备养成记录博客的习惯了,所以又玩了玩,回来记录一下 首先新建一个类,便于调用 public class Lucene ...
- WORD文档书签管理
最近在浏览一个word超长文档,在文档中有几处要点用颜色做了标记,但是在下次查找的时候无法定位,还得一页页去翻,而且无法通过目录概览的形式查看总共做了多少处标记 于是想到了书签 原本以为在视图中能够设 ...
- HTTP协议--cookie、session、缓存与代理
1 Cookie和 Session Cookie和 Session都为了用来保存状态信息,都是保存客户端状态的机制,它们都是为了解决 HTTP无状态的问题而所做的努力. Session可以用 Cook ...
- 牛散NO.3:MACD放之四海 假作真时真亦假
大宗商品日线“异曲同工夺命勾魂枪” 话说有实战意义的技术在任何资本市场里都能产生出神奇的效果.不能说放之四海皆准,但至少起到触类旁通的“牵强”吧.大宗商品特别是在国际市场交易的大宗 商品由于是来自各方 ...
- linux后台运行命令
Ctrl+z/bg/nohup/setsid/& screen 区别待续
- linux系统日志报错
使用journalctl -xe命令时报错: Error was encountered while opening journal files: Input/output error 日志文件损坏了 ...
- Redis之最大内存置换策略
0.前言 Redis默认最大内存大小是应用程序可访问的内存大小, 32位windows下是2GB, linux下是3GB. 64位下可以访问的内存为2^64字节, Redis提供了maxmemory字 ...
- C6455 CSL_EMIF详解
C6455 CSL_EMIF详解 原网址http://www.61ic.com/Article/C6000/C64X/201303/47507.html C6455CSL详解 和DSP6455的EMI ...
- 【转载】表单验证<AngularJs>
原文地址:http://www.cnblogs.com/rohelm/archive/2014/10/19/4033513.html 常用的表单验证指令 1. 必填项验证 某个表单输入是否已填写,只要 ...
- 605. Can Place Flowers【easy】
605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are plante ...