原题传送门

考虑一个一个将字母加入字符串后面

设\(f[i][0/1/2]\)表示长度为\(i\)字符串末尾有\(0/1/2\)个A的种类数

易知:

\(f[1][0]=1,f[1][1]=1,f[1][2]=0\)

\(f[i][0]=f[i-1][0]+f[i-1][1]+f[i-1][2]\)

\(f[i][1]=f[i-1][0]\)

\(f[i][2]=f[i-1][1]\)

发现这个递推式子珂以用矩阵乘法

\[ \left[
\begin{matrix}
f[i][0] & f[i][1] & f[i][2]
\end{matrix}
\right]
*
\left[
\begin{matrix}
1 & 1 & 0 \\
1 & 0 & 1 \\
1 & 0 & 0
\end{matrix}
\right]
=
\left[
\begin{matrix}
f[i+1][0] & f[i+1][1] & f[i+1][2]
\end{matrix}
\right]
\]

矩阵快速幂即可,答案是\(f[n][0]+f[n][1]+f[n][2]\)

#include <bits/stdc++.h>
#define mod 19260817
//#define getchar nc
using namespace std;
inline char nc(){
static char buf[100000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int read()
{
register int x=0,f=1;register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return x*f;
}
inline void write(register int x)
{
if(!x)putchar('0');if(x<0)x=-x,putchar('-');
static int sta[20];register int tot=0;
while(x)sta[tot++]=x%10,x/=10;
while(tot)putchar(sta[--tot]+48);
}
struct mat{
int a[3][3];
inline mat()
{
memset(a,0,sizeof(a));
}
inline mat operator*(const mat&b)const{
mat c;
for(register int i=0;i<3;++i)
for(register int j=0;j<3;++j)
for(register int k=0;k<3;++k)
c.a[i][j]=(c.a[i][j]+1ll*a[i][k]*b.a[k][j])%mod;
return c;
}
}s,o,ans;
inline mat fastpow(register mat a,register int b)
{
mat res;
res.a[0][0]=res.a[1][1]=res.a[2][2]=1;
while(b)
{
if(b&1)
res=res*a;
a=a*a;
b>>=1;
}
return res;
}
int T,n;
int main()
{
T=read();
s.a[0][0]=s.a[1][0]=s.a[2][0]=s.a[0][1]=s.a[1][2]=1;
o.a[0][0]=o.a[0][1]=1;
while(T--)
{
n=read();
ans=o*fastpow(s,n-1);
write((ans.a[0][0]+ans.a[0][1]+ans.a[0][2])%mod),puts("");
}
return 0;
}

【题解】Luogu P4838 P哥破解密码的更多相关文章

  1. [Luogu] P4838 P哥破解密码

    题目背景 P哥是一个经常丢密码条的男孩子. 在ION 8102赛场上,P哥又弄丢了密码条,笔试满分的他当然知道这可是要扣5分作为惩罚的,于是他开始破解ION Xunil系统的密码. 题目描述 定义一个 ...

  2. 洛谷 P4838 P哥破解密码 题解

    矩阵乘法 + 快速幂优化递推: 看到这个题目我们不难想到递推,题干中说3个连续的A出现在序列中是不合法的,所以可以分为三种情况: (1):序列前只有一个A,如:BA,BBA,BABA. (2):序列前 ...

  3. P4838 P哥破解密码

    题目背景 P哥是一个经常丢密码条的男孩子. 在ION 8102赛场上,P哥又弄丢了密码条,笔试满分的他当然知道这可是要扣5分作为惩罚的,于是他开始破解ION Xunil系统的密码. 题目描述 定义一个 ...

  4. [洛谷P4838]P哥破解密码

    题目大意:求长度为$n$的$01$串中,没有连续至少$3$个$1$的串的个数 题解:令$a_1$为结尾一个$1$的串个数,$a_2$为结尾两个$1$的串的个数,$b$为结尾是$0$的串的个数.$a_1 ...

  5. 【洛谷T37388】P哥破解密码

    原题图: 看到这个题,首先想到的当然是暴力打表找规律了 表: 1 2 2 4 3 7 4 13 5 24 6 44 7 81 8 149 9 274 10 504 11 927 12 1705 13 ...

  6. 【UOJ#74】【UR #6】破解密码

    [UOJ#74][UR #6]破解密码 题面 UOJ 题解 发现这个过程是一个字符串哈希的过程. 把第一位单独拿出来考虑,假设这个串是\(p+S\),旋转后变成了\(S+p\). 其哈希值分别是:\( ...

  7. 为什么现在更多需要用的是 GPU 而不是 CPU,比如挖矿甚至破解密码?

    作者:Cascade链接:https://www.zhihu.com/question/21231074/answer/20701124来源:知乎著作权归作者所有,转载请联系作者获得授权. 想要理解G ...

  8. python 暴力破解密码脚本

    python 暴力破解密码脚本 以下,仅为个人测试代码,环境也是测试环境,暴力破解原理都是一样的, 假设要暴力破解登陆网站www.a.com 用户 testUser的密码, 首先,该网站登陆的验证要支 ...

  9. 破解密码那些事儿(Hacking Secret Ciphers with Python)

    作者:Al Sweigart   我们在电视和电影里头经常能够看到黑客们兴奋的快速敲击键盘,接着毫无意义的数字就在屏幕上飞奔(比如黑客帝国).然后让大家产生了一种奇妙的错觉,做黑客是一件高大上的事情, ...

随机推荐

  1. 转载:SVD

    ComputeSVD        在分布式矩阵有CoordinateMatirx, RowMatrix, IndexedRowMatrix三种.除了CoordinateMatrix之外,Indexe ...

  2. 使用ADO.NET

    Program using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

  3. 京东Java架构师讲解购物车的原理及Java实现

    今天来写一下关于购物车的东西, 这里首先抛出四个问题: 1)用户没登陆用户名和密码,添加商品, 关闭浏览器再打开后 不登录用户名和密码问:购物车商品还在吗? 2)用户登陆了用户名密码,添加商品,关闭浏 ...

  4. OpenFOAM——具有压差的平行平板间流动(泊肃叶流动)

    本算例翻译整理自:http://the-foam-house5.webnode.es/products/chapter-1-plane-parallel-plates-case/ 这个算例中两平板间没 ...

  5. 升级pip后,出现ImportError:cannot import name main

    升级pip后,出现ImportError错误,如下图: 解决方法: sudo gedit /usr/bin/pip 进去后修改为 from pip import __main__ if __name_ ...

  6. [转]IntelliJ IDEA 2019 上手

    原文地址:https://www.jianshu.com/p/77f81d5fcf02 一.聊一聊Java IDE 作为程序员,经常会看到这么一类的话题:文本编辑器与IDE哪家强.常见的文本编辑器如E ...

  7. 爬虫的正则表达式re模块

    爬虫一共就四个主要步骤: 明确目标 (要知道你准备在哪个范围或者网站去搜索) 爬 (将所有的网站的内容全部爬下来) 取 (去掉对我们没用处的数据) 处理数据(按照我们想要的方式存储和使用) 对于dow ...

  8. tqdm学习-一个快速,可扩展的Python和CLI进度条

    参考:https://pypi.org/project/tqdm/ 1.安装: (base) userdeMacBook-Pro:~ user$ conda activate deeplearning ...

  9. [LeetCode] 199. Binary Tree Right Side View 二叉树的右侧视图

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  10. Linux安全学习

    安全游戏说明 游戏形式帮助初学者学习和实践网络安全,主要是Linux shell命令的练习. https://overthewire.org/wargames/ 二进制 灰帽黑客进阶秘籍之一--lin ...