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. windows搭建Selenium

    安装 pip install -U selenium 安装浏览器驱动 用不同的浏览器需要安装不同的驱动,驱动放置的路径添加到path中. Firefox geodriver Chrome 下载驱动Ch ...

  2. NCTF2018_easy_audit->coding_breaks

    easy_audit 题目源码 <?php highlight_file(__FILE__); error_reporting(0); if($_REQUEST){ foreach ($_REQ ...

  3. 1-JVM基础

    1-JVM基础 java源码文件,通过javac 转换成class文件. 找到.java文件 词法分析器 tokens流 语法分析器 语义分析器 字节码生成器 转成.class文件 装载 根据全限定路 ...

  4. Junit借助Groboutils Core进行并发测试

    本文参考:http://www.voidcn.com/article/p-ybnvuffh-ke.html:转载请注明出处 junit是无法进行并发测试,但是又有需要并发测试的场景怎么办呢?此时可以借 ...

  5. 数据库SQL---数据库系统概论

    1.基本术语 1)信息:指数据加工处理后有用的数据. 2)信息的3种世界: (1)现实世界:存在于人脑之外的客观世界. (2)信息世界:现实世界在人脑中的反映. (3)数据世界:将信息世界中的信息通过 ...

  6. Scrapy中的crawlspider

    crawlspider 能自动的获取url并提交请求 命令:scrapy genspider -t crawl spidername 'example.cn' 所导入的模块 # -*- coding: ...

  7. phpstudy xdebug 配置

    来源:https://baijiahao.baidu.com/s?id=1607680791440431678&wfr=spider&for=pc https://www.cnblog ...

  8. Apache Commons Lang &#187; 3.10使用简介

    ============================================================= 行文介绍: 1.诞生背景 2.引入方案 3.简单介绍 4 .详情介绍 文档: ...

  9. 基于NFS共享存储实现KVM虚拟机动态迁移

    基于NFS共享存储实现KVM虚拟机动态迁移 一:配置环境 二:安装相关的依赖包 三:实现NFS共享存储 四:KVM机配置相同的步骤 五:安装KVM01安装虚拟机 六:实现迁移  实验初始配置:所有主机 ...

  10. socket编程-多个客户端向服务器发送人脸照片,服务器返回识别结果(服务器使用多线程)...

    recognition.py import numpy as np import face_recognition import os class recognition: def __init__( ...