(hdu 6030) Happy Necklace 找规律+矩阵快速幂
题目链接 :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 找规律+矩阵快速幂的更多相关文章
- 2017ACM暑期多校联合训练 - Team 2 1006 HDU 6050 Funny Function (找规律 矩阵快速幂)
题目链接 Problem Description Function Fx,ysatisfies: For given integers N and M,calculate Fm,1 modulo 1e ...
- 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 ...
- 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 ...
- HDU-6030 Happy Necklace 打表+矩阵快速幂
Happy Necklace 前天个人赛规律都找出来了,n的范围是\(10^{18}\),我一想GG,肯定是矩阵快速幂,然后就放弃了. 昨天学了一下矩阵快速幂. 题意 现在小Q要为他的女朋友一个有n个 ...
- HDU 2855 斐波那契+矩阵快速幂
http://acm.hdu.edu.cn/showproblem.php?pid=2855 化简这个公式,多写出几组就会发现规律 d[n]=F[2*n] 后面的任务就是矩阵快速幂拍一个斐波那契模板出 ...
- 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 ...
- HDU 3292 【佩尔方程求解 && 矩阵快速幂】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=3292 No more tricks, Mr Nanguo Time Limit: 3000/1000 M ...
- 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 ...
- hdu 4565 So Easy! (共轭构造+矩阵快速幂)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4565 题目大意: 给出a,b,n,m,求出的值, 解题思路: 因为题目中出现了开根号,和向上取整后求 ...
随机推荐
- Autorelease机制讲解
Autorelease机制是在iOS内存管理中的一员.在MRC中,是通过调用[obj autorelease]来延迟内存释放:在ARC中,我们已经完全不需要知道Autorelease就能很好地管理好内 ...
- [前端]AngularJS 簡易物件修改入門
各位好,今天要來介紹如何簡單的修改網站上AngularJS相關Application的內容 進而做到某些效果.(警告!所有的Web Application都應該在後端加上相關驗證) 透過本篇你可以簡單 ...
- Oracle day02 函数
order by关键字作用:用于对查询结果进行排序 用法: 1.利用asc .desc对排序列进行升序或降序 2.order by后可以添加多个列(逗号分隔),当一个列的值相同时,在按第二 ...
- Android开发——获得Json数据,并显示图片
流程介绍 使用okhttp网络框架进行get请求,获得json数据 //一个封装好的工具类的静态方法 public static void sendOkHttpRequest(final String ...
- [leetcode](4.21)4. 有效子数组的数目
给定一个整数数组 A,返回满足下面条件的 非空.连续 子数组的数目: 子数组中,最左侧的元素不大于其他元素. 示例 1: 输入:[1,4,2,5,3] 输出:11 解释:有 11 个有效子数组,分别是 ...
- 内部类访问局部变量为什么必须要用final修饰
内部类访问局部变量为什么必须要用final修饰 看了大概五六篇博客, 讲的内容都差不多, 讲的内容也都很对, 但我觉得有些跑题了 略叙一下 String s = "hello"; ...
- break,return和continue三者区别(Java)
一.break用于完全结束一个循环,跳出循环体. 不管是哪种循环,一旦在循环体中遇到break,系统将完全结束循环,开始执行循环之后的代码. class Demo3 { public static v ...
- react 源码之setState
今天看了react源码,仅以记录. 1:monorepo (react 的代码管理方式) 与multirepo 相对. monorepo是单代码仓库, 是把所有相关项目都集中在一个代码仓库中,每个mo ...
- git push origin与git push -u origin master的区别
$ git push origin 上面命令表示,将当前分支推送到origin主机的对应分支. 如果当前分支只有一个追踪分支,那么主机名都可以省略. $ git push 如果当前分支与多个主机存在追 ...
- 2-SAT速成
本文只做总结性说明 2-SAT 2-SAT是k-SAT问题的一种,k-SAT问题在\(k>=3\)时已经被证明是NP完全问题 2-SAT问题定义比较简单 有n个布尔变量\(x_1-x_n\).给 ...