codeforces E. Okabe and El Psy Kongroo(dp+矩阵快速幂)
题目链接:http://codeforces.com/contest/821/problem/E
题意:我们现在位于(0,0)处,目标是走到(K,0)处。每一次我们都可以从(x,y)走到(x+1,y-1)或者(x+1,y)或者(x+1,y+1)三个位子之一。
现在一共有N段线段,每条线段都是平行于X轴的。我们如果此时x是在这段线段之内的话,我们此时走到的点(x,y)需要满足0<=y<=Ci.
现在保证一段线段的终点,一定是下一段线段的起点。问我们从起点走到终点的行走方案数。
题解:简单的dp+矩阵快速幂的模版
显然如果k很小是个很简单的dp,dp[i][j]=dp[i-1][j]+dp[i-1][j-1]+dp[i-1][j+1]。但是k很大所以就要用到矩阵快速幂,一般像这种递推方程式都是可以化为用矩阵来求的
dp[1] 110000000000000 predp[1]
dp[2] 111000000000000 predp[2]
dp[3] 011100000000000 predp[3]
dp[4] 001110000000000 predp[4]
.
.
.
dp[15] 000000000000011 predp[15]
#include <iostream>
#include <cstring>
#include <cstdio>
#define mod 1000000007
using namespace std;
typedef long long ll;
struct Matrix {
ll dp[17][17];
};
Matrix Mul(Matrix a , Matrix b , ll Max) {
Matrix c;
memset(c.dp , 0 , sizeof(c.dp));
for(ll i = 0 ; i <= Max ; i++) {
for(ll j = 0 ; j <= Max ; j++) {
for(int k = 0 ; k <= Max ; k++) {
c.dp[i][j] += ((a.dp[i][k]) % mod * (b.dp[k][j]) % mod) % mod;
c.dp[i][j] %= mod;
}
}
}
return c;
}
Matrix Matrix_quick_pow(Matrix a , ll k , ll Max) {
Matrix res;
memset(res.dp , 0 , sizeof(res.dp));
for(ll i = 0 ; i <= Max ; i++) res.dp[i][i] = 1;
while(k) {
if(k & 1) res = Mul(res , a , Max);
k >>= 1;
a = Mul(a , a , Max);
}
return res;
}
int main() {
ll n , k;
scanf("%lld%lld" , &n , &k);
Matrix ans , ope , pre;
memset(ope.dp , 0 , sizeof(ope.dp));
memset(pre.dp , 0 , sizeof(pre.dp));
for(int i = 0 ; i < 16 ; i++) {
if(i == 0) {
ope.dp[i][i] = 1;
ope.dp[i][i + 1] = 1;
}
else if(i == 15) {
ope.dp[i][i] = 1;
ope.dp[i][i - 1] = 1;
}
else {
ope.dp[i][i] = 1;
ope.dp[i][i + 1] = 1;
ope.dp[i][i - 1] = 1;
}
}
pre.dp[0][0] = 1;
for(int i = 1 ; i <= n ; i++) {
ll a , b , Max , flag = 0;
scanf("%lld%lld%lld" , &a , &b , &Max);
if(b > k) {b = k , flag = 1;}
ans = Matrix_quick_pow(ope , b - a , Max);
for(ll j = Max + 1 ; j < 16 ; j++) pre.dp[j][0] = 0;
ans = Mul(ans , pre , Max);
for(ll j = 0 ; j <= Max ; j++) {
pre.dp[j][0] = ans.dp[j][0];
}
if(flag) break;
}
printf("%lld\n" , ans.dp[0][0]);
return 0;
}
codeforces E. Okabe and El Psy Kongroo(dp+矩阵快速幂)的更多相关文章
- Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo DP+矩阵快速幂加速
E. Okabe and El Psy Kongroo Okabe likes to take walks but knows that spies from the Organization ...
- Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo dp+矩阵快速幂
E. Okabe and El Psy Kongroo Okabe likes to take walks but knows that spies from the Organization c ...
- Codeforces 821E Okabe and El Psy Kongroo(矩阵快速幂)
E. Okabe and El Psy Kongroo time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Codeforces 621E Wet Shark and Block【dp + 矩阵快速幂】
题意: 有b个blocks,每个blocks都有n个相同的0~9的数字,如果从第一个block选1,从第二个block选2,那么就构成12,问对于给定的n,b有多少种构成方案使最后模x的余数为k. 分 ...
- Codeforces 821E Okabe and El Psy Kongroo
题意:我们现在位于(0,0)处,目标是走到(K,0)处.每一次我们都可以从(x,y)走到(x+1,y-1)或者(x+1,y)或者(x+1,y+1)三个位子之一.现在一共有N段线段,每条线段都是平行于X ...
- Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo 矩阵快速幂优化dp
E. Okabe and El Psy Kongroo time limit per test 2 seconds memory limit per test 256 megabytes input ...
- [codeforces821E]Okabe and El Psy Kongroo
题意:(0,0)走到(k,0),每一部分有一条线段作为上界,求方案数. 解题关键:dp+矩阵快速幂,盗个图,注意ll 关于那条语句为什么不加也可以,因为我的矩阵C,就是因为多传了了len的原因,其他位 ...
- bnuoj 34985 Elegant String DP+矩阵快速幂
题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 We define a kind of strings as elegant s ...
- HDU 5434 Peace small elephant 状压dp+矩阵快速幂
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5434 Peace small elephant Accepts: 38 Submissions: ...
随机推荐
- 【iOS】The filename 未命名.ipa in the package contains an invalid character(s)
提交 APP 到苹果官网审核时遇到了这个问题,如图: 其实就是不支持中文,随便换个英文名就行了. 参考:http://blog.csdn.net/u011439689/article/details/ ...
- python的enumerate lambda isinstance filter函数
0x01:filter(function,iterable) filter()函数用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表. 接收两个参数,第一个为函数,第二个为序列(可迭 ...
- SpringBoot Jar包瘦身 - 跟大文件说再见!
前言 SpringBoot部署起来配置非常少,如果服务器部署在公司内网,上传速度还行,但是如果部署在公网(阿里云等云服务器上),部署起来实在头疼.就是 编译出来的 Jar 包很大,如果工程引入了许多开 ...
- oracle RAC LOG_ARCHIVE_DEST_1 与 LOG_ARCHIVE_DEST 冲突解决
在做 oracle RAC 归档日志配置时,出现了一个错误,开始看资料的时候, 注意到了 LOG_ARCHIVE_DEST_n 与 LOG_ARCHIVE_DEST 不能同时使用, 但在配置的时候并没 ...
- fiddler设置断点
1.有两种方法设置断点 before response:也就是发送请求之后,但是Fiddler代理中转之前,这时可以修改请求的数据 after response:也就是服务器响应之后,但是在Fiddl ...
- 【JDK】JDK源码分析-CyclicBarrier
概述 CyclicBarrier 是并发包中的一个工具类,它的典型应用场景为:几个线程执行完任务后,执行另一个线程(回调函数,可选),然后继续下一轮,如此往复. 打个通俗的比方,可以把 CyclicB ...
- 一个web前端开发者的日常唠叨
时间飞逝,距离上一次更新博客已经过去了三个月,上一篇博客的发布时间停留在了4月4日. 近来三个月没有更新博客,深感抱歉和愧疚.停更博客就意味着学习的越来越少,作为一个普通的前端开发者来说这是万万不可取 ...
- 配置Windows Server 2008环境
上一章已经把Windows Server2008操作系统安装完毕,接下来配置一下Windows Server环境.配置网络和共享中心.配置桌面环境.配置用户IE设置.安装Telnet远程工具.配置文件 ...
- Docker入门-搭建docker私有仓库
Docker Hub 目前Docker官方维护了一个公共仓库Docker Hub,其中已经包括了数量超过15000个镜像.大部分需求都可以通过在Docker Hub中直接下载镜像来使用. 注册登录 可 ...
- pip安装时使用国内源,加快下载速度
国内源: 新版ubuntu要求使用https源,要注意. 清华:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:http://mirrors.aliyun.c ...