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 ...
随机推荐
- mybatis返回新增对象的主键
加这两行就可以返回这个插入对象的自增的主键<insert id="insertSeatPortraitData" parameterType="seatPortra ...
- 【leetcode】1048. Longest String Chain
题目如下: Given a list of words, each word consists of English lowercase letters. Let's say word1 is a p ...
- 对webpack的初步研究4
Mode string module.exports = { mode: 'production' }; webpack --mode=production The following string ...
- layer.js插件
官方网址: http://layer.layui.com/
- win server2008关闭危险端口445,135,137,138,139的方法
在Windows server 2008系统上,有两种途经可以禁用本地端口: 1.通过Windows防火墙(比较简单,设置方便) 2.通过IP安全策略(比较复杂,功能强大,不依赖防火墙) 一.通过Wi ...
- 导航栏图标切换:click事件,hover事件
最近再做一个基于angular6的项目,导航栏需求:1.hover切换图标 2.click切换图标 先用jquery实现功能,在在angular组件里面实现. demo如下: <!DOCTYPE ...
- save change is not permitted
https://support.microsoft.com/en-my/help/956176/error-message-when-you-try-to-save-a-table-in-sql-se ...
- javaSE javaEE javaME的区别、有什么不同?
http://zhidao.baidu.com/link?url=oFEPOmW8BnQ0M0w0krS9DyMA5UCUufgHJWV45r9UQZ-0vp_IOx-Yl-VV0hZQ-vHXGYo ...
- delphi之猥琐的webserver实现
http://www.birdol.com/cainiaobiancheng/238.html delphi之猥琐的webserver实现 菜鸟编程 十五楼的鸟儿 7年前 (2009-01-01) ...
- 安装mysql数据库-centos7
mysql官网下载地址:https://dev.mysql.com/downloads/mysql/ 参考安装:https://blog.51cto.com/snowlai/2140451?sourc ...