Codeforces Round #420 (Div. 2) - E
题目链接:http://codeforces.com/contest/821/problem/E
题意:起初在(0,0),现在要求走到(k,0),问你存在多少种走法。 其中有n条线段,每条线段为(a,y)->(b,y),代表如果x坐标走到[a,b]之间时,处于的y坐标要小于这个线段的y坐标,就是只能在线段的下方走(包括刚好在线段上),并且前一个段线段的x坐标与后一个线段的x坐标相同。
思路:dp[i][j]代表从起点走到(i,j)时的走法,那么dp[i][j]=dp[i-1][j]+dp[i-1][j-1]+dp[i-1][j+1]。由于dp[i]只由dp[i-1]转移过来,所以可以忽略i这一维,由于y坐标比较少,x坐标比较大,所以通过矩阵快速幂来优化。
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<time.h>
#include<stack>
#include<cmath>
using namespace std;
typedef long long int LL;
const LL INF = ;
const int MAXN = + ;
const int MAXX = + ;
const int mod = 1e9 + ;
struct Node{
LL l, r, y;
}P[MAXN];
struct Matrix{
int row, col;
LL m[MAXX][MAXX];
void init(int row, int col){
this->row = row;
this->col = col;
for (int i = ; i < row; ++i)
for (int j = ; j < col; ++j)
m[i][j] = ;
}
}C;
Matrix operator*(const Matrix & a, const Matrix& b){
Matrix res;
res.init(a.row, b.col);
for (int k = ; k < a.col; ++k){
for (int i = ; i < res.row; ++i){
if (a.m[i][k] == ) continue;
for (int j = ; j < res.col; ++j){
if (b.m[k][j] == ) continue;
res.m[i][j] = (a.m[i][k] * b.m[k][j] + res.m[i][j]) % mod;
}
}
}
return res;
}
Matrix operator+(const Matrix & a, const Matrix& b){
Matrix res;
res.init(a.row, b.col);
for (int i = ; i< a.col; ++i){
for (int j = ; j < res.row; ++j){
res.m[i][j] = (a.m[i][j] + b.m[i][j]) % mod;
}
}
return res;
}
Matrix Mpow(Matrix A, LL n){
Matrix ans = A, p = A;
while (n){
if (n & ){
ans = ans*p; n--;
}
n >>= ; p = p*p;
}
return ans;
}
int main(){
//#ifdef kirito
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
//#endif
// int start = clock();
int n; LL k;
while (~scanf("%d%lld",&n,&k)){
for (int i = ; i < n; i++){
scanf("%lld%lld%lld", &P[i].l, &P[i].r, &P[i].y);
}
C.init(, ); C.m[][] = ;
for (int i = ; i < n; i++){
if (P[i].l >= k){
break;
}
Matrix res; res.init(, );
for (int j = ; j < ; j++){
if (j>P[i].y){
break;
}
for (int q = -; q <= ; q++){
if (j + q >= && j + q <= P[i].y){
res.m[j][j + q] = ;
}
}
}
res = Mpow(res, min(k,P[i].r) - min(k,P[i].l)-); //超过k的不需计算
for (int j = P[i].y + ; j < ; j++){ //超过线段y坐标的走法不存在,置0
C.m[j][] = ;
}
C = C*res;
}
printf("%lld\n", C.m[][]);
}
//#ifdef LOCAL_TIME
// cout << "[Finished in " << clock() - start << " ms]" << endl;
//#endif
return ;
}
Codeforces Round #420 (Div. 2) - E的更多相关文章
- 【Codeforces Round #420 (Div. 2) C】Okabe and Boxes
[题目链接]:http://codeforces.com/contest/821/problem/C [题意] 给你2*n个操作; 包括把1..n中的某一个数压入栈顶,以及把栈顶元素弹出; 保证压入和 ...
- 【Codeforces Round #420 (Div. 2) B】Okabe and Banana Trees
[题目链接]:http://codeforces.com/contest/821/problem/B [题意] 当(x,y)这个坐标中,x和y都为整数的时候; 这个坐标上会有x+y根香蕉; 然后给你一 ...
- 【Codeforces Round #420 (Div. 2) A】Okabe and Future Gadget Laboratory
[题目链接]:http://codeforces.com/contest/821/problem/A [题意] 给你一个n*n的数组; 然后问你,是不是每个位置(x,y); 都能找到一个同一行的元素q ...
- Codeforces Round #420 (Div. 2) - C
题目链接:http://codeforces.com/contest/821/problem/C 题意:起初有一个栈,给定2*n个命令,其中n个命令是往栈加入元素,另外n个命令是从栈中取出元素.你可以 ...
- Codeforces Round #420 (Div. 2) - B
题目链接:http://codeforces.com/contest/821/problem/B 题意:二维每个整点坐标(x,y)拥有的香蕉数量为x+y,现在给你一个直线方程的m和b参数,让你找一个位 ...
- Codeforces Round #420 (Div. 2) - A
题目链接:http://codeforces.com/contest/821/problem/A 题意:给定一个n*n的矩阵. 问你这个矩阵是否满足矩阵里的元素除了1以外,其他元素都可以在该元素的行和 ...
- Codeforces Round #420 (Div. 2)
/*************************************************************************************************** ...
- Codeforces Round #420 (Div. 2) A,B,C
A. Okabe and Future Gadget Laboratory time limit per test 2 seconds memory limit per test 256 megaby ...
- 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 ...
随机推荐
- VUE 生成二维码插件
原文:https://www.jianshu.com/p/496fd1cbee8d npm install qrcodejs2 --save 页面中引入 dom 结构 JS 方法编写 export d ...
- man LVCREATE
LVCREATE(8) LVCREATE(8) NAME/名称 lvcreat ...
- linux运维、架构之路-keepalived高可用
一.Keepalived介绍 Keepalived起初是专为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP功能,Ke ...
- P哥的桶(线段树+线性基)
https://www.luogu.org/problem/P4839 题目: 有两个操作 1 a b 在a的位置添加b数值 (注意一个位置可以有多个值) 2 a b : 在 a到b的范围任取任意 ...
- HDU 4321 Arcane Numbers 2
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4321 ----------------------------------------------- ...
- Red Hat Linux下安装JDK
1. 下载Linux平台的JDK 下载对应操作系统的jdk,操作系统是32位的就下32位的jdk,64位的就下64位的jdk.下错了装不上的. 下载地址:http://www.Oracle.com/t ...
- JSP 学习笔记1
JSP 学习笔记 JSP是Servlet的一种特殊形式,每个JSP页面就是一个Servlet实例--JSP页面有系统编译成Servlet,Servlet再负责响应用户请求. 1.JSP注释 < ...
- jmeter两种录制方式
jmeter两种录制方式 一.Badboy Badboy是一款不错web自动化测试工具,利用它来录制脚本,并且录制的脚本可以直接保存为JMeter文件来使用. Badboy version 2.1. ...
- HDU 2063 过山车 (匈牙利算法)
题目链接:HDU 2063 Problem Description RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩 ...
- java web中各种context的关系
我举得这篇文章解决了我的很多疑惑,理清了我以前不太清楚的Context关系,读懂这篇文章很有助于理解源码, 原文链接在这里:https://www.jianshu.com/p/2537e2fec546 ...