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是如何与这些特性联系上的 ...
随机推荐
- Struts2学习笔记——Struts2搭建和第一个小程序
1.新建web项目 2.配置Struts2核心过滤器 (1)打开web.xml文件,做以下配置: <?xml version="1.0" encoding="UTF ...
- css块元素的 display 属性 inline-block 的应用
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> < ...
- Python—面向对象01
1.如何使用类 # 先定义类 class LuffyStudent(): school = "luffycity" # 数据属性 def learn(self): # 函数属性 p ...
- 第二章:RESTful API
学习内容 使用Spring MVC编写Restful API 使用Spring MVC处理其他web应用常见的需求和场景 如何处理静态资源和异常,如何使用Spring MVC的拦截器,文件的上传下载, ...
- jquery获取父级元素、子级元素、兄弟元素
1:$(this).parent(expr) 找父亲节点,可以传入expr进行过滤,比如$("span").parent()或者$("span").parent ...
- 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛--I-填空题
链接:https://www.nowcoder.com/acm/contest/90/I 来源:牛客网 1.题目描述 牛客网是是一个专注于程序员的学习和成长的专业平台,集笔面试系统.课程教育.社群交流 ...
- 【TOJ 1912】487-3279(hash+map)
描述 Businesses like to have memorable telephone numbers. One way to make a telephone number memorable ...
- ethereum Pet Shop
在部署宠物商店时遇到的一些坑,给大家总结一下,以免大家多走弯路 转载的地址(详细):https://steemit.com/cn/@lucia3/ethereum-pet-shop 启动"n ...
- 使用deque保留有限的记录
# 使用deque保留有限的记录 >>> from collections import deque >>> q = deque(maxlen=3) # 指定队列的 ...
- you don't have permission to access forbidden
前几天装一个phpStudy 集成环境,打开测试页面的时候突然出现如下错误: 有一些小总结. 一些小的开发测试在本地开发的话,直接localhost/file 就可以, 如果涉及到大的开发环境,一 ...