题目链接: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坐标比较大,所以通过矩阵快速幂来优化。

  1. #define _CRT_SECURE_NO_DEPRECATE
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<algorithm>
  6. #include<string>
  7. #include<queue>
  8. #include<vector>
  9. #include<time.h>
  10. #include<stack>
  11. #include<cmath>
  12. using namespace std;
  13. typedef long long int LL;
  14. const LL INF = ;
  15. const int MAXN = + ;
  16. const int MAXX = + ;
  17. const int mod = 1e9 + ;
  18. struct Node{
  19. LL l, r, y;
  20. }P[MAXN];
  21. struct Matrix{
  22. int row, col;
  23. LL m[MAXX][MAXX];
  24. void init(int row, int col){
  25. this->row = row;
  26. this->col = col;
  27. for (int i = ; i < row; ++i)
  28. for (int j = ; j < col; ++j)
  29. m[i][j] = ;
  30. }
  31. }C;
  32. Matrix operator*(const Matrix & a, const Matrix& b){
  33. Matrix res;
  34. res.init(a.row, b.col);
  35. for (int k = ; k < a.col; ++k){
  36. for (int i = ; i < res.row; ++i){
  37. if (a.m[i][k] == ) continue;
  38. for (int j = ; j < res.col; ++j){
  39. if (b.m[k][j] == ) continue;
  40. res.m[i][j] = (a.m[i][k] * b.m[k][j] + res.m[i][j]) % mod;
  41. }
  42. }
  43. }
  44. return res;
  45. }
  46. Matrix operator+(const Matrix & a, const Matrix& b){
  47. Matrix res;
  48. res.init(a.row, b.col);
  49. for (int i = ; i< a.col; ++i){
  50. for (int j = ; j < res.row; ++j){
  51. res.m[i][j] = (a.m[i][j] + b.m[i][j]) % mod;
  52. }
  53. }
  54. return res;
  55. }
  56. Matrix Mpow(Matrix A, LL n){
  57. Matrix ans = A, p = A;
  58. while (n){
  59. if (n & ){
  60. ans = ans*p; n--;
  61. }
  62. n >>= ; p = p*p;
  63. }
  64. return ans;
  65. }
  66. int main(){
  67. //#ifdef kirito
  68. // freopen("in.txt", "r", stdin);
  69. // freopen("out.txt", "w", stdout);
  70. //#endif
  71. // int start = clock();
  72. int n; LL k;
  73. while (~scanf("%d%lld",&n,&k)){
  74. for (int i = ; i < n; i++){
  75. scanf("%lld%lld%lld", &P[i].l, &P[i].r, &P[i].y);
  76. }
  77. C.init(, ); C.m[][] = ;
  78. for (int i = ; i < n; i++){
  79. if (P[i].l >= k){
  80. break;
  81. }
  82. Matrix res; res.init(, );
  83. for (int j = ; j < ; j++){
  84. if (j>P[i].y){
  85. break;
  86. }
  87. for (int q = -; q <= ; q++){
  88. if (j + q >= && j + q <= P[i].y){
  89. res.m[j][j + q] = ;
  90. }
  91. }
  92. }
  93. res = Mpow(res, min(k,P[i].r) - min(k,P[i].l)-); //超过k的不需计算
  94. for (int j = P[i].y + ; j < ; j++){ //超过线段y坐标的走法不存在,置0
  95. C.m[j][] = ;
  96. }
  97. C = C*res;
  98. }
  99. printf("%lld\n", C.m[][]);
  100. }
  101. //#ifdef LOCAL_TIME
  102. // cout << "[Finished in " << clock() - start << " ms]" << endl;
  103. //#endif
  104. return ;
  105. }

Codeforces Round #420 (Div. 2) - E的更多相关文章

  1. 【Codeforces Round #420 (Div. 2) C】Okabe and Boxes

    [题目链接]:http://codeforces.com/contest/821/problem/C [题意] 给你2*n个操作; 包括把1..n中的某一个数压入栈顶,以及把栈顶元素弹出; 保证压入和 ...

  2. 【Codeforces Round #420 (Div. 2) B】Okabe and Banana Trees

    [题目链接]:http://codeforces.com/contest/821/problem/B [题意] 当(x,y)这个坐标中,x和y都为整数的时候; 这个坐标上会有x+y根香蕉; 然后给你一 ...

  3. 【Codeforces Round #420 (Div. 2) A】Okabe and Future Gadget Laboratory

    [题目链接]:http://codeforces.com/contest/821/problem/A [题意] 给你一个n*n的数组; 然后问你,是不是每个位置(x,y); 都能找到一个同一行的元素q ...

  4. Codeforces Round #420 (Div. 2) - C

    题目链接:http://codeforces.com/contest/821/problem/C 题意:起初有一个栈,给定2*n个命令,其中n个命令是往栈加入元素,另外n个命令是从栈中取出元素.你可以 ...

  5. Codeforces Round #420 (Div. 2) - B

    题目链接:http://codeforces.com/contest/821/problem/B 题意:二维每个整点坐标(x,y)拥有的香蕉数量为x+y,现在给你一个直线方程的m和b参数,让你找一个位 ...

  6. Codeforces Round #420 (Div. 2) - A

    题目链接:http://codeforces.com/contest/821/problem/A 题意:给定一个n*n的矩阵. 问你这个矩阵是否满足矩阵里的元素除了1以外,其他元素都可以在该元素的行和 ...

  7. Codeforces Round #420 (Div. 2)

    /*************************************************************************************************** ...

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

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

随机推荐

  1. HTML计算机代码元素

    计算机代码 1 2 3 4 5 6 var person = {     firstName:"Bill",     lastName:"Gates",     ...

  2. CodeForces - 35D

    题目:https://vjudge.net/contest/326867#problem/A 题意:有一个农场,自己有m斤粮食,有n天,每天动物吃的量不同,那个动物的食量的是由他是从那天开始进这个农场 ...

  3. php面试专题---MySQL分表

    php面试专题---MySQL分表 一.总结 一句话总结: 分库分表要数据达到一定的量级才用,这样才有效率,不然利不一定大于弊,可能会增加一次I/O消耗 1.分库分表的使用量级是多少? 单表行数超过 ...

  4. statistic_action

    方差variance 统计中的方差(样本方差)是每个样本值与全体样本值的平均数之差的平方值的平均数.V 离差平方和(Sum of Squares of Deviations)是各项与平均项之差的平方的 ...

  5. JS-闭包(Closures)和let声明块级作用域变量

    闭包: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Closures 闭包是函数和声明该函数的词法环境的组合. let: https ...

  6. DR 项目小结

    前言 个人的项目总结, 非技术类博文. 需要补充的知识点 HTTP 协议与其内置方法 curl 指令和各选项的意义 Keystone 认证流程和各项目配置文件 [keystone_authtoken] ...

  7. 进程之间的通讯Queue简单应用

    #进程间通讯--Queue #Process有时需要通信的,操作系统提供了很多机制来实现进程之间的通讯 #而Queue就是其中一个 #1.Queue的使用 #可以使用multiprocessing模块 ...

  8. 利用多态,简易实现电脑usb连接设备案例

    package cn.learn.Practice03; public interface UsbInterface { void open(); //打开usb void close(); //关闭 ...

  9. (转载)Solr4.x在Tomcat下的部署

    Step1 下载安装包: 下载最新版本安装包 点击此处下载Tomcat    点击此处下载Solr Step2 解压: 解压Tomcat和Solr Step3 拷贝War包: 拷贝\solr-4.x\ ...

  10. vue 使用 computed 结合 filter 实现数据的的过滤和排序

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...