题目链接 :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. cocos creator主程入门教程(一)—— 初识creator

    五邑隐侠,本名关健昌,10年游戏生涯,现隐居五邑.本系列文章以TypeScript为介绍语言. 我们在cocos creator新建一个Hello TypeScript项目,都会有一个assets/S ...

  2. php编写生成酷炫验证码

    <?php $im=imagecreate(200,100);//生成画布 imagecolorallocate($im,0,0,0);//背景色 $white=imagecoloralloca ...

  3. 聊聊 API Gateway 和 Netflix Zuul

    最近参与了公司 API Gateway 的搭建工作,技术选型是 Netflix Zuul,主要聊一聊其中的一些心得和体会. 本文主要是介绍使用 Zuul 且在不强制使用其他 Neflix OSS 组件 ...

  4. Fundebug前端JavaScript插件更新至1.6.0,新增test()方法用于测试

    摘要: 1.6.0新增fundebug.test()方法用于测试,请大家及时更新. 默认情况下,Fundebug 插件能够自动捕获未处理的错误(uncaught error).另外,开发者也可以通过使 ...

  5. maven 聚合

    聚合很简单, 在父 pom 中写出子 pom 文件的路径即可 <name>parent Maven Webapp</name> <!-- FIXME change it ...

  6. .Net Core + Angular Cli / Angular4 开发环境搭建

    一.基础环境配置 1.安装VS 2017 v15.3或以上版本 2.安装VS Code最新版本 3.安装Node.js v6.9以上版本 4.重置全局npm源,修正为 淘宝的 NPM 镜像: npm  ...

  7. Android使用Mob ShareSDK 分享不同平台

    Share分享,要注意的是,如果不能调用起微信相关,看看微信开发者平台是否添加正式,微信开发者正式之后,打正式包进行分享调用 /*分享弹框*/ private void showShare2() { ...

  8. C# 用户控件之温度计

    本文以一个用户控件[User Control]实现温度计的小例子,简述用户控件的相关知识,以供学习分享使用,如有不足之处,还请指正. 概述 一般而言,用户控件[User Control],是在Visu ...

  9. Redis进阶学习笔记

    Redis是什么这里不用再说了吧?下面是官方的解释 Redis is an open source (BSD licensed), in-memory data structure store, us ...

  10. Docker 镜像编排并部署SpringBoot应用

    Docker-compose是一个基于Docker的编排工具,所谓编排个人理解就是将不同的镜像通过配置,组成一个新的运行环境,官方定义是:Compose is a tool for defining ...