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 ...
随机推荐
- 【leetcode】1093. Statistics from a Large Sample
题目如下: We sampled integers between 0 and 255, and stored the results in an array count: count[k] is ...
- python实现Restful服务(基于flask)(2)
参考:https://blog.csdn.net/yelena_11/article/details/53404892 最简单的post例子: from flask import Flask, req ...
- plafrom SDK
{ //http://www.alipay-seller.mpymnt.com/node/82 //https://blog.csdn.net/xiaopingping1234567/article/ ...
- 英语单词vendors
vendors 来源——https://www.docker.com/products/docker-hub Share and Collaborate with Docker Hub Docker ...
- URL跳转漏洞
URL跳转原理: 由于越来越多的需要和其他第三方应用交互,以及在自身应用内部根据不同的逻辑将用户引向到不同的页面,譬如一个典型的登录接口就经常需要在认证成功之后将用户引导到登录之前的页面,整个过程中如 ...
- python 全栈开发,Day9(函数的初始,返回值,传参,三元运算)
一.函数的初始 比如python没有len()方法,如果求字符串的长度 使用for循环 s = 'asdfadsf' count = 0 for i in s: count += 1 print(co ...
- bzoj 做起走 -- bzoj 1009 GT 考试
现在每次做一道bzoj上的题,整个人都感觉升华了... 先是在网上各种搜题解.要么只有代码,要么有点讲解看不懂,对于从来没有耐心看完别人代码的我,只能一篇一篇的翻..然后终于在某2011级同学的某段话 ...
- Guid--调用Guid.genID();
package com.ufgov.ar.common.util; import java.net.InetAddress; /** * <p> * Title: 产生唯一标识 /** * ...
- Factors and Multiples
Factors and Multiples PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Y ...
- iOS逆向一个APP指令集
一.脱壳获取.app文件 1.查询壳有没加密 otool -l mac-o文件 | grep crypt 2.Clutch -i Clutch -d num 3.脱壳后的位置 /private/v ...