题目链接: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+矩阵快速幂)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. Codeforces 621E Wet Shark and Block【dp + 矩阵快速幂】

    题意: 有b个blocks,每个blocks都有n个相同的0~9的数字,如果从第一个block选1,从第二个block选2,那么就构成12,问对于给定的n,b有多少种构成方案使最后模x的余数为k. 分 ...

  5. 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 ...

  6. 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 ...

  7. [codeforces821E]Okabe and El Psy Kongroo

    题意:(0,0)走到(k,0),每一部分有一条线段作为上界,求方案数. 解题关键:dp+矩阵快速幂,盗个图,注意ll 关于那条语句为什么不加也可以,因为我的矩阵C,就是因为多传了了len的原因,其他位 ...

  8. 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 ...

  9. HDU 5434 Peace small elephant 状压dp+矩阵快速幂

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5434 Peace small elephant  Accepts: 38  Submissions: ...

随机推荐

  1. js数组排序 多条件

    按照[次数]和[时间]排序,选择次数最多的排在前面,同样次数的情况下时间较新排在前面. 原始数据: var arr= [ {name:'qqq', num:2,time:'2015-06-08 13: ...

  2. 抓取崩溃的log日志

    1.下载adb工具包 也就是解锁软件,如果要解锁的话,需确认有fastboot 安装jdk.sdk 2.注意事项 请确保电脑上只连接了一台手机设备(最好只连接一条USB线),同时确保手机已开启USB调 ...

  3. 在表格中添加text便加框

    private void createTableText(Table table) { TableEditor editor = new TableEditor(table); for (int i ...

  4. Asp.Net Core 发布到 Docker(Linux Centos 虚拟机,使用Dockerfile)

    实践一下 Asp.Net Core (基于.net core 2.2)部署到Docker 一.准备工作: 1. 使用Virtualbox创建一个Centos系统的虚拟机,并安装docker和vim 2 ...

  5. JavaSE(一)Java程序的三个基本规则-组织形式,编译运行,命名规则

    一.Java程序的组织形式       Java程序是一种纯粹的面向对象的程序设计语言,因此Java程序必须以类(class)的形式存在,类(class)是Java程序的最小程序单位.       J ...

  6. maven打jar包包括依赖包

    <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId& ...

  7. abp(net core)+easyui+efcore实现仓储管理系统——使用 WEBAPI实现CURD (十三)

    abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+ ...

  8. viewpager_轮播

    public class MainActivity extends Activity { private ViewPager pager; private int[] id={R.layout.lay ...

  9. Appium+python自动化(三十二)- 代码写死一时爽,框架重构火葬场 - PageObject+unittest(超详解)

    简介 江湖有言:”代码写死一时爽,框架重构火葬场“,更有人戏言:”代码动态一时爽,一直动态一直爽

  10. 100天搞定机器学习|day39 Tensorflow Keras手写数字识别

    提示:建议先看day36-38的内容 TensorFlow™ 是一个采用数据流图(data flow graphs),用于数值计算的开源软件库.节点(Nodes)在图中表示数学操作,图中的线(edge ...