HDU 6030(矩阵快速幂+规律)

news/2024/9/23 9:30:34/

传送门
题目描述:

Happy Necklace

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1146    Accepted Submission(s): 491


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(1T10000), denoting the number of test cases.
For each test case, there is a single line containing an integer n(2n1018), 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
 
223

Sample Output
 
34

Source
2017中国大学生程序设计竞赛 - 女生专场

题目描述:你有一个有n个颜色为红色或蓝色的珠子的项链,你可以将项链截断成长度为素数的珠子,如果截取出的一段红色的珠子的个数大于蓝色的珠子,则成为good,问一共有多少种good的可能性。
比赛的时候,当拿到这道题的时候,完全一脸懵逼。打了好一会才意识到可以打表,故打只,可以发现
n=2,res=3;n=3,res=4;n=4,res=6;n=5,res=9;
故有递推式 An=An-1+An-3;

赛后问了下师兄,发现正确的推断方法:

    如果用a表示红色,用b表示蓝色。题意明显可以看出只需要管长度2和3的连续序列是否符合!

    如果以b结尾,那么下一个必须是a,或者加个aab就可以了!

所以同理就可以推出递推式An=An-1+An-3;
看一下题目的数据范围,n最大1e18,因此常规O(n)的递推显然不可行,因此直接上O(logn)的矩阵快速幂。

发现递推式是四阶的递推式,故所得的常数矩阵应该是四维的。之后只需带入矩阵快速幂模板即可。
#include <bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
typedef long long ll;
struct martix{ll mo[4][4];martix(){memset(mo,0,sizeof(mo));}
};
martix mul(martix a,martix b){martix c;for(int i=0;i<4;i++){for(int j=0;j<4;j++){for(int k=0;k<4;k++){c.mo[i][j]=(c.mo[i][j]+a.mo[i][k]*b.mo[k][j])%mod;}}}return c;
}
ll powmod(martix a,ll n){martix T;for(int i=0;i<4;i++){T.mo[i][i]=1;}while(n){if(n&1) T=mul(a,T);n>>=1;a=mul(a,a);}return (T.mo[0][0]*6+T.mo[0][1]*4+T.mo[0][2]*3)%mod;
}
int main()
{ll t;cin>>t;while(t--){ll n;cin>>n;int ans[5]={0,0,3,4,6};if(n<=4){cout<<ans[n]%mod<<endl;}else{martix q;q.mo[0][0]=q.mo[0][2]=q.mo[3][2]=q.mo[1][0]=q.mo[2][1]=q.mo[3][2]=1;cout<<powmod(q,n-4)%mod<<endl;}}return 0;
}


http://www.ppmy.cn/news/289323.html

相关文章

IntelliJ IDEA pycharm webstorm 激活

前端&#xff0c;后端&#xff0c;爬虫全套激活&#xff1a; IntelliJ IDEA 2017.2.5 破解 激活 http://xidea.online/?/download/intellij-idea 最新的pycharm 破解 激活 http://www.imsxm.com/jetbrains-license-server.html http://www.imsxm.com 最新的webstorm破解 激活 …

开博寄语

在这个特殊的日子开通了博客&#xff0c;今天是阴历的中元节&#xff0c;又是我的生日&#xff0c;已届不惑&#xff0c;混迹IT20多年&#xff0c;欢喜过、失落过、迷惘过&#xff0c;在这里开一片小小的天地&#xff0c;记录自己的心路历程&#xff0c;与有缘人共勉&#xff0…

1068 Find More Coins (30分)

文章目录 1 题目2 解析2.1 题意2.2 思路 3 参考代码 1 题目 1068 Find More Coins (30分) Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds o…

听音室-HIFI入门之400多张发烧碟中选出的精品

折腾这些年&#xff0c;也算对这个Hi-Fi,即所谓的音响发烧有些认识&#xff0c;这里大谈得比较多的是音响器材&#xff0c;Hi-end器材动辄几十万&#xff0c;有的上百万&#xff0c;为什么我们花这么多精力和金钱的投入&#xff1f;是因为高保真的音乐才是我们追求的本质&#…

pcm5102a解码芯片音质评测_WHAT HIFI推荐2020年最值得购买解码器:11款器材上榜

下面是《What Hi-Fi》推荐的2020年最值得购买解码器,来看看有哪些上榜器材吧! 一、和弦Chord Qutest便携式解码器 查看 和弦Chord Qutest便携式解码器 评测>> 目前该产品有“HIFI说影音城”商家在售,点击查看>> 口碑评说: “听完了上述曲目,我可以明显感受到Q…

[zz] 高端HIFI发烧音频DAC解码芯片排名

音频“解码器”中最核心、重要的器件&#xff0c;无非就是“解码”&#xff08;DAC&#xff0c;数模转换&#xff09;芯片了&#xff0c;大家常常很关注音频DAC芯片的选用&#xff0c;也热衷于对其优劣的讨论。 本文尝试对当前最优秀的高端音频DAC芯片的结构、技术和性能等做简…

真无线蓝牙耳机也能享受HIFI音质,2021五大高人气TWS蓝牙耳机推荐

当代生活&#xff0c;智能手机的耳机孔已经变成了奢侈品。而相对的&#xff0c;有线耳机的市场也被无线耳机不断蚕食。 22 日&#xff0c;光大证券发布了一个有关于目前真无线耳机的行业调查报告。从有一根线连着的颈戴式耳机&#xff0c;再到现在主流的真无线式耳机。这个行业…

STM32H7做的HIFI DSD音乐播放器

历时多年&#xff0c;真是艰辛&#xff0c;幸好做出来了。目前支持的音乐格式如下&#xff1a; WAV&#xff1a;16位/24位/32位 - 8K / 11.025K / 16K / 22.05K / 24K / 32K / 44.1K / 48K / 88.2K / 96K/176.4K / 192K / 352.8K / 384K/705.6K/768K PCM 5.1声道 WAV&#xf…