(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,求出的值, 解题思路: 因为题目中出现了开根号,和向上取整后求 ...
随机推荐
- Spring Cloud Alibaba基础教程:Nacos配置的多环境管理
前情回顾: <Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现> <Spring Cloud Alibaba基础教程:支持的几种服务消费方式> ...
- 一统江湖的大前端(7)React.js-从开发者到工程师
目录 一. 前端打怪升级指南 1.1 我应该从哪个框架开始学? 1.2 一次转职 1.3 二次转职 1.4 转职-其他 二. 为什么你应该学习React 2.1 技术栈的延伸 2.2 组件化开发 2. ...
- .NET读取json数据并绑定到对象
需要引用的命名空间: 读取的具体应用: this代表本实体(对象),通过PopulateObject,直接将读取到的json数据与对象进行绑定 Json保存的具体应用: 将对象保存为Json JObj ...
- VS 使用vs2017自带的诊断工具(Diagnostic Tools)诊断程序的内存问题
前言 一般来说.NET程序员是不用担心内存分配释放问题的,因为有垃圾收集器(GC)会自动帮你处理.但是GC只能收集那些不再使用的内存(根据对象是否被其它活动的对象所引用)来确定.所以如果代码编写不当的 ...
- [leetcode](4.21)4. 有效子数组的数目
给定一个整数数组 A,返回满足下面条件的 非空.连续 子数组的数目: 子数组中,最左侧的元素不大于其他元素. 示例 1: 输入:[1,4,2,5,3] 输出:11 解释:有 11 个有效子数组,分别是 ...
- java-同步控制及不可变设置(只读访问)
1.还是直接上代码简单了解一下: package com.synchronize.test; import java.util.ArrayList; import java.util.Collecti ...
- HTML5移动端拖动惯性
下面代码只是实现了上下滑动惯性,没有写水平滑动惯性.(临时代码笔记,可能会在以后的过程中不断更新优化代码) /** * 惯性原理: * 产生的速度 = 移动距离 / 移动时间 * 距离 = 松开的坐标 ...
- JS判断类型
JS中的typeof方法可以查看数据的类型,如下: console.log(typeof 2); // number console.log(typeof "2"); // str ...
- Access denied for user 'root'@'localhost' (using password:YES) Mysql5.7
解决方案: (1) 打开MySQL目录下的my.ini文件,在文件的最后添加一行“skip-grant-tables”,保存并关闭文件.(my.ini在C:\ProgramData\MySQL\MyS ...
- 六、Drawable
Drawable表示的是一种可以在Canvas上进行绘制的抽象的概念. 1.Drawable简介 Drawable是一个抽象类,是所有Drawable对象的基类,每个具体的Drawable都是它的子类 ...