HDU-6030 Happy Necklace 打表+矩阵快速幂
Happy Necklace
前天个人赛规律都找出来了,n的范围是\(10^{18}\),我一想GG,肯定是矩阵快速幂,然后就放弃了。
昨天学了一下矩阵快速幂。
题意
现在小Q要为他的女朋友一个有n个宝石的好的项链(直线),定义好的项链为:如果任意素数长的子串中蓝宝石的数量不小于红宝石的数量就是好的。小Q可以买多少种好的项链呢?
思路
n这么大,绝对是规律题。
先简单分析一下,其实对于好项链的定义可以化简为任意长度为3的子串中至少有两个蓝宝石,然后打表。
#include<bits/stdc++.h>
using namespace std;
int arr[100],pre[100];
int check(int cnt)
{
for(int i =1; i<=cnt; i++)
{
pre[i]=pre[i-1]+(arr[i]==1);
if(i>=3&&(pre[i]-pre[i-3])<2)
return 0;
}
return 1;
}
int main()
{
for(int i=3; i<=20; i++)
{
int ans=0;
for(int j=0; j<(1<<i); j++)
{
for(int k=1; k<=i; k++)
arr[k]=0;
int tmp=j,cnt=0;
while(tmp)
{
arr[++cnt]=tmp%2;
tmp/=2;
}
if(check(i))
ans++;
}
printf("%d %d\n",i,ans);
}
return 0;
}
3 4
4 6
5 9
6 13
7 19
8 28
9 41
10 60
11 88
12 129
13 189
14 277
15 406
16 595
17 872
18 1278
19 1873
20 2745
可以发现a[i]=a[i-1]+a[i-3]
矩阵构造如下:
\(\left[\begin{matrix}1 & 0 & 1\\1 & 0 & 0\\0 & 1 & 0\end{matrix}\right]*\left[\begin{matrix}a_{n-1}\\a_{n-2}\\a_{n-3}\end{matrix}\right]=\left[\begin{matrix}a_{n-1}+a_{n-3} \\a_{n-1}\\a_{n-2}\end{matrix}\right]=\left[\begin{matrix}a_{n} \\a_{n-1}\\a_{n-2}\end{matrix}\right]\)
称最左边的矩阵为关系矩阵A,已知1 2 3 的值分别为2 3 4 ,
\]
代码
//#include<bits/stdc++.h>
#include<vector>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<math.h>
#include<queue>
#include<map>
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e6+10;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
const double eps=1e-14;
struct Matrix
{
ll mat[3][3];
};
Matrix multi(Matrix a,Matrix b)
{
Matrix res;
memset(res.mat,0,sizeof(res.mat));
for(ll i=0; i<3; i++)
{
for(ll j=0; j<3; j++)
{
for(ll k=0; k<3; k++)
{
res.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
res.mat[i][j]%=mod;
}
}
}
return res;
}
Matrix qpow(Matrix a,ll b)
{
Matrix ans;
memset(ans.mat,0,sizeof(ans.mat));
for(ll i=0; i<3; i++) ans.mat[i][i]=1;//单位矩阵
while(b)
{
if(b&1)
ans=multi(ans,a);
b>>=1;
a=multi(a,a);
}
return ans;
}
int main()
{
Matrix base;
memset(base.mat,0,sizeof(base.mat));
base.mat[0][0]=base.mat[0][2]=base.mat[1][0]=base.mat[2][1]=1;
ll T;
scanf("%lld",&T);
while(T--)
{
ll n;
scanf("%lld",&n);
if(n==2) printf("3\n");
else if(n==3) printf("4\n");
else
{
Matrix now=qpow(base,n-3);
printf("%lld\n",(now.mat[0][0]*4%mod+now.mat[0][1]*3%mod+now.mat[0][2]*2%mod)%mod);
}
}
return 0;
}
HDU-6030 Happy Necklace 打表+矩阵快速幂的更多相关文章
- (hdu 6030) Happy Necklace 找规律+矩阵快速幂
题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=6030 Problem Description Little Q wants to buy a nec ...
- 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,求出的值, 解题思路: 因为题目中出现了开根号,和向上取整后求 ...
- 数学--数论--HDU 2802 F(N) 公式推导或矩阵快速幂
Giving the N, can you tell me the answer of F(N)? Input Each test case contains a single integer N(1 ...
- HDU 2256 Problem of Precision 数论矩阵快速幂
题目要求求出(√2+√3)2n的整数部分再mod 1024. (√2+√3)2n=(5+2√6)n 如果直接计算,用double存值,当n很大的时候,精度损失会变大,无法得到想要的结果. 我们发现(5 ...
- hdu 1757 A Simple Math Problem_矩阵快速幂
题意:略 简单的矩阵快速幂就行了 #include <iostream> #include <cstdio> #include <cstring> using na ...
随机推荐
- D. AB-string
https://codeforces.com/contest/1238/problem/D 题目大意:统计good string的个数,good string的定义,给定的字符串中含有回文段落, 题解 ...
- Largest Rectangle in a Histogram 杭电1506
题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=1506 Problem Description A histogram is a polygon co ...
- js多线程的实现
我们都知道JS是一种单线程语言,即使是一些异步的事件也是在JS的主线程上运行的(具体是怎么运行的,可以看我另一篇博客JS代码运行机制).像setTimeout.ajax的异步请求,或者是dom元素的一 ...
- 今天开始让我们一起来学JavaScript吧!(今天先扯先别的)
1.为什么要学习JavaScript? 首先它是web开发人员必须学习的3门语言之一: ①HTML定义了网页的内容 ②CSS描述了网页的布局: ③JavaScript网页的行为 首先JavaScrip ...
- Linux学习笔记(五)搜索命令
搜索命令 whereis which locate find whereis 英文原意:locate the binary,source,and manual page files for a com ...
- vue-element-admin执行npm install 报错
如果你出现这类报错: 那么恭喜你,因为这个问题很好解决. ----------------------- 解决方法: git config --global url."https://&qu ...
- 实现Nginx Upload 模块 功能上传文件。
分析(也许我表达的让人难以理解,但是我想说一句,直接实践是最好的.....): 一.Ningx 上传( 1.安装Nginx 的模块文件(upload):https://www.nginx.com/re ...
- Jest 前端单元测试工具
Jest和enzyme 前端单元测试工具 什么是Jest? Jest是一个令人愉悦的JavaScript测试框架,其重点是简单性. 它适用于使用以下项目的项目:Babel,TypeScript,Nod ...
- git在用https进行push时候免输账密的方法
先新建一个文件 $ touch ~/.git-credentials $ vim ~/.git-credentials 进去添加内容(github为github.com,码云为gitee.com) h ...
- (第一篇)linux简介与发展历史以及软件的安装
1.Linux操作系统基本结构介绍: 操作系统: 英文名称Operating System,简称OS,是计算机系统中必不可少的基础系统软件,它是应用程序运行以及用户操作必备的基础环境支撑,是计算机系统 ...