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. 【转】线程同步------java synchronized详解

    Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码. 一.当两个并发线程访问同一个对象object中的这个synchronized(this ...

  2. Python学习笔记(四)多进程的使用

    python中多进程与Linux 下的C基本相同.   fork的基本使用   先看最简单的例子: # coding: utf-8 import os def my_fork(): pid = os. ...

  3. Spring 常用注入注解(annotation)和其对应xml标签

    使用注解需要修改bean.xml: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=& ...

  4. jrebel license server 激活方法

    方法1: 使用已经封装好的jar包,保持一直运行即可(放到服务器上). 链接:https://pan.baidu.com/s/1rrn-6F26JpD5RSsbJV3-hQ 密码: dscu 服务器上 ...

  5. 使用apache POI解析Excel文件

    1. Apache POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能. 2. POI结构 ...

  6. AngularJs学习笔记(4)——自定义指令

    对指令的第一印象:它是一个自定义标签! 先来看一个简单的指令: <!doctype html> <html ng-app="myApp"> <head ...

  7. Redis之Hash数据结构

    0.前言 redis是KV型的内存数据库, 数据库存储的核心就是Hash表, 我们执行select命令选择一个存储的db之后, 所有的操作都是以hash表为基础的, 下面会分析下redis的hash数 ...

  8. 基于RocketIO的高速串行协议设计与实现

    随着对信息流量需求的不断增长, 传统并行接口技术成为进一步提高数据传输速率的瓶颈.过去主要用于光纤通信的串行通信技术—SERDES正在取代传统并行总线而成为高速接口技术的主流.SERDES 是串行器) ...

  9. 基于FPGA实现的高速串行交换模块实现方法研究

    基于FPGA实现的高速串行交换模块实现方法研究 https://wenku.baidu.com/view/9a3d501a227916888486d7ed.html

  10. 【转载】浏览器加载和渲染html的顺序

    1.浏览器加载和渲染html的顺序 1.IE下载的顺序是从上到下,渲染的顺序也是从上到下,下载和渲染是同时进行的.2.在渲染到页面的某一部分时,其上面的所有部分都已经下载完成(并不是说所有相关联的元素 ...