hdu5863 cjj's string game
矩阵快速幂
#include<bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
int N,M,K; struct Matrix{
int a[25][25];
int n;
Matrix(int _n=0){
memset(a,0,sizeof(a));
n = _n;
}
Matrix operator *(const Matrix &T) const{
Matrix ans = Matrix(n);
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
for(int k = 0; k < n; ++k)
ans.a[i][j] = (ans.a[i][j] + 1ll*a[i][k]*T.a[k][j]%MOD) % MOD;
return ans;
}
};
Matrix operator ^(Matrix x,int y){
Matrix ans = Matrix(x.n);
for(int i = 0; i < x.n; ++i) ans.a[i][i] = 1;
while(y){
if(y&1) ans=ans*x;
y >>= 1; x = x*x;
}
return ans;
} int main(){
int T; scanf("%d",&T);
while(T--) {
scanf("%d %d %d",&N,&M,&K);
Matrix A = Matrix(M*2+1);
A.a[0][0] = 1;
Matrix P = Matrix(M*2+1);
for(int i = 0; i < M; ++i) P.a[i][0] = K*K-K;
for(int i = 1; i < M; ++i) P.a[i-1][i] = K;
P.a[M-1][M*2] = K; P.a[M*2-1][M*2] = K;
for(int i = M; i < M*2+1; ++i) P.a[i][M] = K*K-K;
for(int i = M+1; i < M*2; ++i) P.a[i-1][i] = K; P = P^N;
P = A*P; int ans = 0;
for(int i = M; i < M*2+1; ++i) ans = (ans + P.a[0][i]) % MOD;
printf("%d\n",ans);
}
return 0;
}
hdu5863 cjj's string game的更多相关文章
- HDU5863 cjj's string game(DP + 矩阵快速幂)
题目 Source http://acm.split.hdu.edu.cn/showproblem.php?pid=5863 Description cjj has k kinds of charac ...
- hdu5863 cjj's string game
矩阵快速幂 #include<bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MOD = ...
- HDU 5863 cjj's string game
$dp$,矩阵加速. 设$dp[i][j][0]$表示:长度为$i$的两个字符串,之前还未出现过长度为$m$相同的,目前为止最后$j$个是相同的. 设$dp[i][j][1]$表示:长度为$i$的两个 ...
- HDU 5863 cjj's string game (矩阵乘法优化递推)
题目大意:用k种字符构建两个长度为n的字符串(每种字符有无限多个),要求对应位置字符相同的连续子串最长长度为m,问方法数. 其中k,n,m是输入,n(1<=n<=1000000000), ...
- HDU 5863 cjj's string game ( 16年多校10 G 题、矩阵快速幂优化线性递推DP )
题目链接 题意 : 有种不同的字符,每种字符有无限个,要求用这k种字符构造两个长度为n的字符串a和b,使得a串和b串的最长公共部分长度恰为m,问方案数 分析 : 直觉是DP 不过当时看到 n 很大.但 ...
- Java 字符串 String
什么是Java中的字符串 在 Java 中,字符串被作为 String 类型的对象处理. String 类位于 java.lang 包中.默认情况下,该包被自动导入所有的程序. 创建 String 对 ...
- 2016 Multi-University Training Contest 10
solved 7/11 2016 Multi-University Training Contest 10 题解链接 分类讨论 1001 Median(BH) 题意: 有长度为n排好序的序列,给两段子 ...
- C++ 构造函数_内存分区_对象初始化
内存分区 栈区:int x = 0:int *p = NULL; 定义一个变量,定义一个指针时,会在栈区进行分配内存.分配的内存系统分配收回的,我们不用管. 堆区:int *p = new i ...
- 透过WinDBG的视角看String
摘要 : 最近在博客园里面看到有人在讨论 C# String的一些特性. 大部分情况下是从CODING的角度来讨论String. 本人觉得非常好奇, 在运行时态, String是如何与这些特性联系上的 ...
随机推荐
- oracle 基础知识(四)常用函数
SQL中的单记录函数 .ASCII 返回与指定的字符对应的十进制数; SQL') zero,ascii(' ') space from dual; A A ZERO SPACE --------- - ...
- HDU 2199 Can you solve this equation?(二分解方程)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2199 Can you solve this equation? Time Limit: 2000/10 ...
- HDU 1077 Catching Fish(用单位圆尽可能围住多的点)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1077 Catching Fish Time Limit: 10000/5000 MS (Java/Oth ...
- css的基础用法(下)
定位: <html> <head> <meta charset="utf-8" /> <title>定位</title> ...
- oracle https://localhost:1158/em 无法打开
解决办法一: 首先查看本机Oracle安装路径中 portlist.ini 文件里面的端口号是多少,例如我的就是5500. 那么在浏览器中输入的地址就是:https://localhost:5500/ ...
- 【TOJ 3369】CD(二分)
描述 Jack and Jill have decided to sell some of their Compact Discs, while they still have some value. ...
- 通过匿名管道获取CMD运行结果
#include <iostream> #include <string> #include <Windows.h> using namespace std; /* ...
- SAP存储税率税码的相关表
T007A 稅關鍵字 T007B 會計中的稅務處理 T007C Groupings for tax codes T007D 賦稅群組的內文 T007F 銷項 / 進項群組的稅 T007G 不再使用該表 ...
- 构建高可靠hadoop集群之0-hadoop用户向导
本文翻译自:http://hadoop.apache.org/docs/r2.8.0/hadoop-project-dist/hadoop-hdfs/HdfsUserGuide.html 基于2.8. ...
- ubuntu安装和常用软件推荐
ubuntu安装和常用软件推荐(个人整理) 2016.08.22 17:29 13811浏览 字号 安装一套双系统,win10打游戏,ubuntu开发,win10放机械,ubuntu放固态,电脑联想i ...