hdu 3658 HDU 3658 How many words(矩阵快速幂)
来源:互联网 发布:破解手机收费软件 编辑:程序博客网 时间:2024/06/06 00:23
hdu 3658 HDU 3658 How many words(矩阵快速幂)
题意
长为m的单词,要求相邻字母的ascii码值相差小于等于32且至少有一对相邻的字母,码值相差恰好为32,求这样的单词有多少个,其中m<1e9。
解题思路:
代码
#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>#include<map>#include<string>#include<queue>#include<vector>#include<list>//#pragma comment(linker,"/STACK:1024000000,1024000000")using namespace std;#define INF 0x3f3f3f3fconst long long mod=1000000007;typedef long long Matrix[53][53];void muti(const Matrix a,const Matrix b,Matrix c){ Matrix res; memset(res,0,sizeof res); for(int i=1;i<=52;i++) for(int j=1;j<=52;j++) { if(a[i][j]==0) continue; for(int k=1;k<=52;k++) { res[i][k]+=a[i][j]*b[j][k]; res[i][k]%=mod; } } memcpy(c,res,sizeof res);}void pow_m(const Matrix a,long long n,Matrix c){ Matrix res,b; memcpy(b,a,sizeof b); memset(res,0,sizeof res); for(int i=1;i<=52;i++) res[i][i]=1; while(n) { if(n%2) muti(res,b,res); muti(b,b,b); n/=2; } memcpy(c,res,sizeof res);}void show(Matrix a){ for(int i=1;i<=52;i++) for(int j=1;j<=52;j++) { printf("%d ",a[i][j]); if(j==52) puts(""); }}char str[53];int main(){ int t; for(int i=1;i<=52;i++) if(i<=26) str[i]='A'+i-1; else if(i<=52)str[i]='a'+i-27; else str[i]=str[i-52]; scanf("%d",&t); Matrix trans,trans2; for(int i=1;i<=52;i++) { for(int j=1;j<=52;j++) if(abs(str[i]-str[j])<32) trans[j][i]=1; else trans[j][i]=0; } for(int i=1;i<=52;i++) { for(int j=1;j<=52;j++) if(abs(str[i]-str[j])<=32) trans2[j][i]=1; else trans2[j][i]=0; } Matrix a; memset(a,0,sizeof a); for(int i=1;i<=52;i++) a[1][i]=1; while(t--) { int n; scanf("%d",&n); Matrix res; pow_m(trans,n-1,res); muti(a,res,res); long long ans=0,extra=0; for(int i=1;i<=52;i++) {extra+=res[1][i],extra%=mod;} pow_m(trans2,n-1,res); muti(a,res,res); for(int i=1;i<=52;i++) ans+=res[1][i],ans%=mod; ans=(ans-extra+mod)%mod; printf("%I64d\n",ans); } return 0;}
0 0
- HDU - 3658 How many words 矩阵快速幂
- HDU 3658 How many words (矩阵快速幂&递推)
- hdu 3658 HDU 3658 How many words(矩阵快速幂)
- HDU 3658 How many words
- hdu 3658 How many words
- hdu 3658 How many words dp+矩阵连乘
- HDU 2157 How many ways??(经典矩阵快速幂)
- HDU 2157 How many ways??(矩阵快速幂)
- HDU 2157 How many ways??(矩阵快速幂)
- HDU 2157 How many ways??(矩阵的快速幂)
- HDU 2157 - How many ways??(矩阵快速幂)
- HDU 2157 How many ways??(矩阵快速幂)
- HDU 2157How many ways??(矩阵快速幂)
- HDU3658 How many words 矩阵快速幂
- 【矩阵快速幂】HDU 2157 How many ways??(矩阵快速幂经典问题)
- hdu 2157 How many ways_ 矩阵快速幂
- HDU-2157 How many ways?? 矩阵快速幂
- hdu 2157 How many ways?? (矩阵快速幂)
- section 1.5 numtri
- 5-15 计算圆周率 (15分)
- HTML标记语言介绍
- MYSQL 相关
- 数据结构与算法分析(三) —— 关于树的深入探讨
- hdu 3658 HDU 3658 How many words(矩阵快速幂)
- 【数字图像处理学习笔记之五】形态学开闭运算
- adb 命令
- Android Studio 问题解决记录
- linux学习日记(二)
- Treap学习小计
- Java NIO 学习(三)--FileChannel
- 5、最长公共子序列
- adb shell 获取版本信息