题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=6030

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 +.
Note: The necklace is a single string, {not a circle}. Input
The first line of the input contains an integer T(≤T≤), denoting the number of test cases.
For each test case, there is a single line containing an integer n(≤n≤), 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 +. Sample Input Sample Output Source
2017中国大学生程序设计竞赛 - 女生专场

题目大意:有n个珠子,有红蓝两种珠子 符合下列两个条件的珠子排列方法有多少种?

*******在每串手链种,长度为素数的子串种,红色珠子要不小于蓝色珠子

分析:找到规律:f[i] = f[i-1]+f[i-3];因为n很大   可以用矩阵快速幂写

初始矩阵为2  3  4  转移矩阵为0  0  1

1   0  0

0   1  1

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include <vector>
#include<iostream>
using namespace std;
#define N 50005
#define INF 0x3f3f3f3f
#define LL long long
#define mod 1000000007
struct node
{
LL a[][];
};
node mul(node a,node b)
{
node te;
memset(te.a,,sizeof(te.a));
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
for(int k=;k<;k++)
{
te.a[i][j]+=(a.a[i][k]*b.a[k][j])%mod;
}
}
}
return te;
}
LL quick(LL n)
{
if(n<)
{
int num[]={,,,};
return num[n];
}
node b,team;
b.a[][] = ;b.a[][] = ;b.a[][] = ;
b.a[][] = ;b.a[][] = b.a[][] = ;
b.a[][] = ;b.a[][] = b.a[][] = ;
memset(team.a,,sizeof(team.a));
for(int i=;i<;i++)
team.a[i][i] = ;
LL k = n-;
while(k)
{
if(k&)
team = mul(team,b);
b = mul(b,b);
k = k/;
}
return (team.a[][]*+team.a[][]*+team.a[][]*)%mod;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
LL n;
scanf("%lld",&n);
printf("%lld\n",quick(n));
}
return 0;
}

(hdu 6030) Happy Necklace 找规律+矩阵快速幂的更多相关文章

  1. 2017ACM暑期多校联合训练 - Team 2 1006 HDU 6050 Funny Function (找规律 矩阵快速幂)

    题目链接 Problem Description Function Fx,ysatisfies: For given integers N and M,calculate Fm,1 modulo 1e ...

  2. HDU 1005 Number Sequence【斐波那契数列/循环节找规律/矩阵快速幂/求(A * f(n - 1) + B * f(n - 2)) mod 7】

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  3. HDU - 6198 number number number(规律+矩阵快速幂)

    题意:已知F0 = 0,F1 = 1,Fn = Fn - 1 + Fn - 2(n >= 2), 且若n=Fa1+Fa2+...+Fak where 0≤a1≤a2≤⋯≤a,n为正数,则n为mj ...

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

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

  5. HDU 2855 斐波那契+矩阵快速幂

    http://acm.hdu.edu.cn/showproblem.php?pid=2855 化简这个公式,多写出几组就会发现规律 d[n]=F[2*n] 后面的任务就是矩阵快速幂拍一个斐波那契模板出 ...

  6. HDU 5950:Recursive sequence(矩阵快速幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:给出 a,b,n,递推出 f(n) = f(n-1) + f(n-2) * 2 + n ^ 4. f ...

  7. HDU 3292 【佩尔方程求解 && 矩阵快速幂】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=3292 No more tricks, Mr Nanguo Time Limit: 3000/1000 M ...

  8. HDU - 4965 Fast Matrix Calculation 【矩阵快速幂】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4965 题意 给出两个矩阵 一个A: n * k 一个B: k * n C = A * B M = (A ...

  9. hdu 4565 So Easy! (共轭构造+矩阵快速幂)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4565 题目大意: 给出a,b,n,m,求出的值, 解题思路: 因为题目中出现了开根号,和向上取整后求 ...

随机推荐

  1. [PHP] 抽象类abstract的回顾

    1.abstract定义为抽象的类不能被实例化. 2.它里面至少有一个方法是被声明为抽象的,那么这个类就必须被声明为抽象的. 3.被定义为抽象的方法只是声明了其调用方式(参数),不能定义其具体的功能实 ...

  2. Spring框架基础(下)

    log4J 导入log4J.jar 创建log4J.properties # Create a file called log4j.properties as shown below and plac ...

  3. JavaScript知识点 思维导图

    javascript变量 javascript数据类型 javascript运算符 javascript流程语句 javascript数组 javascript字符串函数 javascript函数基础 ...

  4. loj#6073. 「2017 山东一轮集训 Day5」距离(费用流)

    题意 题目链接 Sol 我们可以把图行列拆开,同时对于行/列拆成很多个联通块,然后考虑每个点所在的行联通块/列联通块的贡献. 可以这样建边 从S向每个行联通块连联通块大小条边,每条边的容量为1,费用为 ...

  5. vue中input输入框的模糊查询实现

    最近在使用vue写webapp在,一些感觉比较有意思的分享一下. 1:input输入框: <input class="s-search-text" placeholder=& ...

  6. 数据库CRUD操作以及MyBatis的配置使用

    • 业务字段设计 • 数据库创建 • CRUD操作 • MyBatis集成 • 注解和XML定义 • ViewObject和DateTool • 首页开发     • 业务字段设计 实体: name: ...

  7. 免费试用MongoDB云数据库 (MongoDB Atlas)教程

    众所周知,MongoDB包括社区版和企业版,但不止如此,MongoDB公司还有MongoDB Atlas:Database as a Service. MongoDB Atlas delivers t ...

  8. ASP.NET Core 入门教程 2、使用ASP.NET Core MVC框架构建Web应用

    一.前言 1.本文主要内容 使用dotnet cli创建基于解决方案(sln+csproj)的项目 使用Visual Studio Code开发基于解决方案(sln+csproj)的项目 Visual ...

  9. Python3漏洞扫描工具 ( Python3 插件式框架 )

    目录 Python3 漏洞检测工具 -- lance screenshot requirements 关键代码 usage documents README Guide Change Log TODO ...

  10. Linux下创建桌面快捷方式

    建立一个文本文件,文件名必须以.desktop结尾,.desktop前面的作为快捷方式的名称 添加如下内容 [Desktop Entry]Encoding=UTF-8Name=PostmanExec= ...