矩阵快速幂

#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的更多相关文章

  1. HDU5863 cjj's string game(DP + 矩阵快速幂)

    题目 Source http://acm.split.hdu.edu.cn/showproblem.php?pid=5863 Description cjj has k kinds of charac ...

  2. hdu5863 cjj's string game

    矩阵快速幂 #include<bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MOD = ...

  3. HDU 5863 cjj's string game

    $dp$,矩阵加速. 设$dp[i][j][0]$表示:长度为$i$的两个字符串,之前还未出现过长度为$m$相同的,目前为止最后$j$个是相同的. 设$dp[i][j][1]$表示:长度为$i$的两个 ...

  4. HDU 5863 cjj's string game (矩阵乘法优化递推)

    题目大意:用k种字符构建两个长度为n的字符串(每种字符有无限多个),要求对应位置字符相同的连续子串最长长度为m,问方法数. 其中k,n,m是输入,n(1<=n<=1000000000), ...

  5. HDU 5863 cjj's string game ( 16年多校10 G 题、矩阵快速幂优化线性递推DP )

    题目链接 题意 : 有种不同的字符,每种字符有无限个,要求用这k种字符构造两个长度为n的字符串a和b,使得a串和b串的最长公共部分长度恰为m,问方案数 分析 : 直觉是DP 不过当时看到 n 很大.但 ...

  6. Java 字符串 String

    什么是Java中的字符串 在 Java 中,字符串被作为 String 类型的对象处理. String 类位于 java.lang 包中.默认情况下,该包被自动导入所有的程序. 创建 String 对 ...

  7. 2016 Multi-University Training Contest 10

    solved 7/11 2016 Multi-University Training Contest 10 题解链接 分类讨论 1001 Median(BH) 题意: 有长度为n排好序的序列,给两段子 ...

  8. C++ 构造函数_内存分区_对象初始化

    内存分区 栈区:int  x = 0:int  *p = NULL; 定义一个变量,定义一个指针时,会在栈区进行分配内存.分配的内存系统分配收回的,我们不用管. 堆区:int  *p = new  i ...

  9. 透过WinDBG的视角看String

    摘要 : 最近在博客园里面看到有人在讨论 C# String的一些特性. 大部分情况下是从CODING的角度来讨论String. 本人觉得非常好奇, 在运行时态, String是如何与这些特性联系上的 ...

随机推荐

  1. 复合词(Compound Words, UVa 10391)(stl set)

    You are to find all the two-word compound words in a dictionary. A two-word compound word is a word i ...

  2. HDU1214 圆桌会议(找规律,数学)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1214 圆桌会议 Time Limit: 2000/1000 MS (Java/Others)    M ...

  3. 【其它】Nook HD刷机

    很久以前的 Nook HD 平板刷机.只能用 microSD(TF)卡刷.需要的软件全都保存在了自己的百度网盘,自己亲测有效. 一.准备工作 1.首先,将tf卡格式化为fat32格式,实测可以使用.将 ...

  4. Python使用dict和set

    dict Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也成为map,使用键-值(key-value)存储,具有极快的查找速度. 假设要根据同学的名字查找对应的 ...

  5. js 事件委托 事件代理

    JavaScript高级程序设计上解释:事件委托就是利用事件冒泡,只指定一个事件处理程序,就可以管理某一类型的所有事件. 通过例子类比: 有三个同事预计会在周一收到快递.为签收快递,有两种办法:一是三 ...

  6. springboot 整合dubbo 消费模块引入springboot 之后自动注入jdbc 模块导致启动报错问题

    方案一: 排除方法 pom文件直接将数据起步模块内排除数据源自动注入 jdbc jar <!--mybatis-plus 集成 --><!--mybitis--><dep ...

  7. Ajax 跨域的几种解决方案

    作者:黄轩链接:http://www.zhihu.com/question/19618769/answer/38934786来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处 ...

  8. python核心编程2 第七章 练习

    7-4. 建立字典. 给定两个长度相同的列表,比如说,列表[1, 2, 3,...]和['abc', 'def','ghi',...],用这两个列表里的所有数据组成一个字典,像这样:{1:'abc', ...

  9. Linux分享笔记:shell终端的介绍

    [1] Linux中 “shell终端” 和 “bash解释器” 的区别 shell终端是通过基于系统调用接口开发出的程序,用来让用户与系统进行对话,管理计Linux系统.它是一个命令行工具,操作类似 ...

  10. jquery添加html代码的几种方法

    经常用jq来DOM添加html代码 就总结了jq里面最常用的动态添加html代码的方法 append在元素内部的尾部加上元素 prepend在元素内部的前部加上元素 after在元素外部的尾部加上元素 ...