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. vue2.x学习笔记(十五)

    接着前面的内容:https://www.cnblogs.com/yanggb/p/12609450.html. 组件的自定义事件 这里来学习一下组件中的自定义事件. 事件名 不同于组件名和prop,事 ...

  2. python 基础篇 匿名函数

    匿名函数基础 首先,什么是匿名函数呢?以下是匿名函数的格式: lambda argument1, argument2,... argumentN : expression 我们可以看到,匿名函数的关键 ...

  3. 用Java代码来校验QQ号

    校验qq号码: 1.要求必须是5-15位数字 2.0不能开头 分析: A:键盘录入一个qq号码 B:写一个功能实现校验 C:调用功能,输出结果. 代码实现:public class RegexDemo ...

  4. 前端基础进阶(七)-前端工程师最容易出错的问题-this关键字

    我们在学习JavaScript的时候,因为对一些概念不是很清楚,但是又会通过一些简洁的方式把它给记下来,那么这样自己记下来的概念和真正的概念产生了很强的偏差. 当然,还有一些以为这个是对的,还会把它发 ...

  5. 2019-2020-1 20199303《Linux内核原理与分析》第六周作业

    系统调用的三层机制 首先是为系统增加新的命令 运行脚本自动生成文件系统 其中有一个显示时间的功能 编辑test.c文件,增加一个hello函数用来显示学号,再次使用make roofts自动编译,调用 ...

  6. 设定程序随windows启动

    Boot Trigger Example (C++) /******************************************************************** Thi ...

  7. [Inno Setup] 执行程序,返回值不为0时提示用户

    procedure LoadPerfCounter(); var Ret : Integer; begin if Exec(ExpandConstant('{sys}') + '\lodctr.exe ...

  8. Python封装应用程序的最佳项目结构是什么?

    Python封装应用程序的最佳项目结构是什么? 转载来源于stackoverflow:https://stackoverflow.com/questions/193161/what-is-the-be ...

  9. flutter在2019年会有怎样的表现?

    2019独角兽企业重金招聘Python工程师标准>>> Flutter的趋势 在移动端,受成本和效率的驱使,跨平台一站式开发慢慢成为一个趋势.从Hybird,RN,WEEX,Flut ...

  10. flex学习

            今天看视频学习的时候,发现css有一个 flex 的属性,非常的好用,为了让自己能够熟记,特意来分享一下flex的用法. 首先,采用Flex布局的元素,称为Flex容器(flex co ...