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 ...
随机推荐
- HTML计算机代码元素
计算机代码 1 2 3 4 5 6 var person = { firstName:"Bill", lastName:"Gates", ...
- CodeForces - 35D
题目:https://vjudge.net/contest/326867#problem/A 题意:有一个农场,自己有m斤粮食,有n天,每天动物吃的量不同,那个动物的食量的是由他是从那天开始进这个农场 ...
- php面试专题---MySQL分表
php面试专题---MySQL分表 一.总结 一句话总结: 分库分表要数据达到一定的量级才用,这样才有效率,不然利不一定大于弊,可能会增加一次I/O消耗 1.分库分表的使用量级是多少? 单表行数超过 ...
- statistic_action
方差variance 统计中的方差(样本方差)是每个样本值与全体样本值的平均数之差的平方值的平均数.V 离差平方和(Sum of Squares of Deviations)是各项与平均项之差的平方的 ...
- JS-闭包(Closures)和let声明块级作用域变量
闭包: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Closures 闭包是函数和声明该函数的词法环境的组合. let: https ...
- DR 项目小结
前言 个人的项目总结, 非技术类博文. 需要补充的知识点 HTTP 协议与其内置方法 curl 指令和各选项的意义 Keystone 认证流程和各项目配置文件 [keystone_authtoken] ...
- 进程之间的通讯Queue简单应用
#进程间通讯--Queue #Process有时需要通信的,操作系统提供了很多机制来实现进程之间的通讯 #而Queue就是其中一个 #1.Queue的使用 #可以使用multiprocessing模块 ...
- 利用多态,简易实现电脑usb连接设备案例
package cn.learn.Practice03; public interface UsbInterface { void open(); //打开usb void close(); //关闭 ...
- (转载)Solr4.x在Tomcat下的部署
Step1 下载安装包: 下载最新版本安装包 点击此处下载Tomcat 点击此处下载Solr Step2 解压: 解压Tomcat和Solr Step3 拷贝War包: 拷贝\solr-4.x\ ...
- vue 使用 computed 结合 filter 实现数据的的过滤和排序
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...