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

2
2
3

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(矩阵快速幂)的更多相关文章

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

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

  2. HDU6030 Happy Necklace(递推+矩阵快速幂)

    传送门:点我 Little Q wants to buy a necklace for his girlfriend. Necklaces are single strings composed of ...

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

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

  4. 矩阵快速幂--HDU 6030 Happy Necklace

    Problem Description Little Q wants to buy a necklace for his girlfriend. Necklaces are single string ...

  5. HDU-6030 Happy Necklace 打表+矩阵快速幂

    Happy Necklace 前天个人赛规律都找出来了,n的范围是\(10^{18}\),我一想GG,肯定是矩阵快速幂,然后就放弃了. 昨天学了一下矩阵快速幂. 题意 现在小Q要为他的女朋友一个有n个 ...

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

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

  7. 51nod 算法马拉松18 B 非010串 矩阵快速幂

    非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...

  8. 51nod 1113 矩阵快速幂

    题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...

  9. 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

    还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...

随机推荐

  1. Zend Framework(一) windows8.1下配置zend framework1.12

    windows8.1下配置zend framework1.12配置步骤: 1.     下载 zend framework1.12库 2.      创建zend frameworkproject 2 ...

  2. linux下统计程序/函数运行时间(转)

    一. 使用time 命令 例如编译一个hello.c文件 #gcc hello.c -o hello 生成了hello可执行文件,此时统计该程序的运行时间便可以使用如下命令 #time ./hello ...

  3. android 开机自启动的几种方法,监听不到RECEIVE_BOOT_COMPLETED的处理办法

    第一种:  监控RECEIVE_BOOT_COMPLETED,即开机启动事件 另外一种: 监控sd卡mount事件  开机总会扫描sd卡吧? 监控sd卡事件也有类似开机启动效果.特别app安装在sd卡 ...

  4. web info

    http://blog.csdn.net/qq_24473141/article/details/51363662 http://blog.sina.com.cn/s/blog_8e392fc2010 ...

  5. eslint — js书写规范

    一.安装 npm install -g eslint 安装eslint 编辑器安装插件eslint(具体安装方法根据不同编辑器而不同) 二.使用 使用方法一: eslint --init npm中用命 ...

  6. iOSXib布局后代码修改约束的值

      如何修改autolayout 约束的值? 目前我已知的方法有5种 1.修改frame(有时候可能会不起作用,但可以做动画) 2.修改约束的float值 3.使用VisualFormat 语言 4. ...

  7. Android Studio 使用笔记: 重命名和重构

    重命名 选中一个变量名称,菜单才是可用状态.然后可以根据系统给出的建议或者自己重新定义变量名称. 快捷键:Shift + F6 (Windows和Mac都是一样的) 重构 选中需要重构的代码,可以按照 ...

  8. php 字符串截取

    $str="3,22,11,444,33,1,3455,33,22,444,55,66,77,88,99,554336,"; echo substr($str,0,strlen($ ...

  9. Ext扩展的QQ表情选择面板

    Ext扩展的QQ表情选择面板 define(function () { EmoteChooser = function(cfg){ this.width=340; this.height=112; t ...

  10. Linux Linux程序练习二

    /* 编写一个程序读取a.txt文件,将文件内容数字从小到大排序,并将排序结果写入b.txt. */ #include <stdio.h> #include <stdlib.h> ...