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 ,

\[\left[\begin{matrix}a_{n}\\a_{n-1}\\a_{n-2}\end{matrix}\right]=A^{n-3}*\left[\begin{matrix}4\\3\\2\end{matrix}\right]
\]

代码

//#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 打表+矩阵快速幂的更多相关文章

  1. (hdu 6030) Happy Necklace 找规律+矩阵快速幂

    题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=6030 Problem Description Little Q wants to buy a nec ...

  2. HDU 2855 斐波那契+矩阵快速幂

    http://acm.hdu.edu.cn/showproblem.php?pid=2855 化简这个公式,多写出几组就会发现规律 d[n]=F[2*n] 后面的任务就是矩阵快速幂拍一个斐波那契模板出 ...

  3. 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 ...

  4. HDU 3292 【佩尔方程求解 && 矩阵快速幂】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=3292 No more tricks, Mr Nanguo Time Limit: 3000/1000 M ...

  5. 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 ...

  6. hdu 4565 So Easy! (共轭构造+矩阵快速幂)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4565 题目大意: 给出a,b,n,m,求出的值, 解题思路: 因为题目中出现了开根号,和向上取整后求 ...

  7. 数学--数论--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 ...

  8. HDU 2256 Problem of Precision 数论矩阵快速幂

    题目要求求出(√2+√3)2n的整数部分再mod 1024. (√2+√3)2n=(5+2√6)n 如果直接计算,用double存值,当n很大的时候,精度损失会变大,无法得到想要的结果. 我们发现(5 ...

  9. hdu 1757 A Simple Math Problem_矩阵快速幂

    题意:略 简单的矩阵快速幂就行了 #include <iostream> #include <cstdio> #include <cstring> using na ...

随机推荐

  1. stand up meeting 12/22/2015 && 用户体验收录

    part 组员                工作              工作耗时/h 明日计划 工作耗时/h    UI 冯晓云  完善页面切换,尝试子页面设计    4  完善页面切换和子页面 ...

  2. 玩转控件:Fucking ERP之流程图

    前言 首先,跟守护在作者公众号和私信作者催更的朋友们道个歉.疫情的原因,公司从年初到现在一直处于996+的高压模式,导致公众号更新频率较低.而且作者每更新一篇原创公众号,既要对自己沉淀知识负责,也要对 ...

  3. golang实现并发爬虫三(用队列调度器实现)

    欲看此文,必先可先看: golang实现并发爬虫一(单任务版本爬虫功能) gollang实现并发爬虫二(简单调度器) 上文中的用简单的调度器实现了并发爬虫. 并且,也提到了这种并发爬虫的实现可以提高爬 ...

  4. 浅析Java7中的ConcurrentHashMap

    引入ConcurrentHashMap 模拟使用hashmap在多线程场景下发生线程不安全现象 import java.util.HashMap; import java.util.Map; impo ...

  5. Hbase的安装与基本操作

    简介: 1安装 HBase​   本节介绍HBase的安装方法,包括下载安装文件.配置环境变量.添加用户权限等. 1.1 下载安装文件   HBase是Hadoop生态系统中的一个组件,但是,Hado ...

  6. Python开发基础之Python常用的数据类型

    一.Python介绍 Python是一种动态解释型的编程语言.Python它简单易学.功能强大.支持面向对象.函数式编程,可以在Windows.Linux等多种操作系统上使用,同时Python可以在J ...

  7. thinkphp--多表查询

    我们可以将两个表连起来一起查询数据,我现在有两张表,一个是feedback表和member表,如图: 总目录: 上代码: $where = array(); $"; $Model = M(' ...

  8. qt tableview 选择模式

    QAbstractItemView::SingleSelection QAbstractItemView::ContiguousSelection QAbstractItemView::Extende ...

  9. 【Django】runserver 0.0.0.0:0 后,究竟发生了什么

    WSGI协议 Django是遵循WSGI协议设计的 WSGI协议主要包括server和application两个部分: WSGI server:负责从客户端接收请求,将request转发给applic ...

  10. Libra教程之:执行Transactions

    文章目录 Transactions是什么 Transactions运行的基础条件 Transactions的结构 执行Transactions Transactions是什么 我们讲到了Libra是一 ...