E. Okabe and El Psy Kongroo
 

Okabe likes to take walks but knows that spies from the Organization could be anywhere; that's why he wants to know how many different walks he can take in his city safely. Okabe's city can be represented as all points (x, y) such that x and y are non-negative. Okabe starts at the origin (point (0, 0)), and needs to reach the point (k, 0). If Okabe is currently at the point (x, y), in one step he can go to (x + 1, y + 1), (x + 1, y), or (x + 1, y - 1).

Additionally, there are n horizontal line segments, the i-th of which goes from x = ai to x = bi inclusive, and is at y = ci. It is guaranteed that a1 = 0, an ≤ k ≤ bn, and ai = bi - 1 for 2 ≤ i ≤ n. The i-th line segment forces Okabe to walk with y-value in the range 0 ≤ y ≤ ciwhen his x value satisfies ai ≤ x ≤ bi, or else he might be spied on. This also means he is required to be under two line segments when one segment ends and another begins.

Okabe now wants to know how many walks there are from the origin to the point (k, 0) satisfying these conditions, modulo 109 + 7.

Input

The first line of input contains the integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 1018) — the number of segments and the destination xcoordinate.

The next n lines contain three space-separated integers aibi, and ci (0 ≤ ai < bi ≤ 1018, 0 ≤ ci ≤ 15) — the left and right ends of a segment, and its y coordinate.

It is guaranteed that a1 = 0, an ≤ k ≤ bn, and ai = bi - 1 for 2 ≤ i ≤ n.

Output

Print the number of walks satisfying the conditions, modulo 1000000007 (109 + 7).

Examples
input
1 3
0 3 3
output
4
input
2 6
0 3 0
3 10 2
output
4
Note

The graph above corresponds to sample 1. The possible walks are:

The graph above corresponds to sample 2. There is only one walk for Okabe to reach (3, 0). After this, the possible walks are:

题意就是让你从(0,0)点走到(k,0)点,有多少种方法,如果你在(x,y)点,你的下一步可以走(x+1, y+1), (x+1,y), 或者 (x+1, y-1).

有几条首尾在x坐标刚好相接的检测线,你在每一个检测线的底下不能超过任何一条检测线的y坐标

网上的大佬看了看数据范围就知道是递推+矩阵快速幂了...

套路题??

果然我还是见得太少了,感觉看完这个题以后思路还是挺妙的利用矩阵快速幂来对递推进行优化

网上盗了一张图,权侵删...

代码如下:

 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=;
typedef struct Matrix
{
ll mat[][];
}matrix;
matrix A,B,pre;
ll n,endd;
ll L,R,y;
Matrix matrix_mul(matrix a,matrix b,ll len)
{
matrix c;
memset(c.mat,,sizeof (c.mat));
for (ll i=;i<=len;++i){
for (ll j=;j<=len;++j){
for (ll k=;k<=len;++k){
c.mat[i][j]+=((a.mat[i][k])%mod*(b.mat[k][j])%mod)%mod;
c.mat[i][j]%=mod;
}
}
}
return c;
}
Matrix matrix_quick_power(matrix a,ll k,ll len)
{
matrix b;
memset(b.mat,,sizeof(b.mat));
for (ll i=;i<=len;++i){
b.mat[i][i]=;//单位矩阵
}
while (k){
if (k%==){
b=matrix_mul(a,b,len);
k-=;
}
else{
a=matrix_mul(a,a,len);
k/=;
}
}
return b;
}
int main(){
while (cin>>n>>endd){
memset(pre.mat,,sizeof (pre.mat));
memset(A.mat,,sizeof (A.mat));
for (ll i=;i<;++i){
for (ll j=i-;j<i+&&j<;++j){
if (j>=&&j<=){
A.mat[i][j]=;
}
}
}
ll flag=;
pre.mat[][]=;
for (ll i=;i<=n;++i){
cin>>L>>R>>y;//读入每个线段
if (R>endd) R=endd,flag=;//如果边界超出了结束的节点就不用多算了
B=matrix_quick_power(A,R-L,y);//让A那个矩阵做一下快速幂
for (ll j=y+;j<=;++j) pre.mat[j][]=;
//trick,如果上一个线段结束时有高于当前线段的起点的部分,这一部分是不能走的,我们把pre的这部分附为0
B=matrix_mul(B,pre,y);
for (ll j=;j<=y;++j)
pre.mat[j][]=B.mat[j][];//我们每次更新一下第一列
if (flag==)
break;
}
cout<<B.mat[][]<<endl;
}
}

Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo dp+矩阵快速幂的更多相关文章

  1. Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo DP+矩阵快速幂加速

    E. Okabe and El Psy Kongroo     Okabe likes to take walks but knows that spies from the Organization ...

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

  3. Codeforces 821E Okabe and El Psy Kongroo(矩阵快速幂)

    E. Okabe and El Psy Kongroo time limit per test 2 seconds memory limit per test 256 megabytes input ...

  4. Codeforces Round #420 (Div. 2)

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

  5. Codeforces Round #420 (Div. 2) A-E

    本来打算划划水洗洗睡了,突然听到这次的主人公是冈部伦太郎 石头门(<steins;gate>)主题的比赛,岂有不打之理! 石头门真的很棒啊!人设也好剧情也赞曲子也特别好听. 推荐http: ...

  6. codeforces E. Okabe and El Psy Kongroo(dp+矩阵快速幂)

    题目链接:http://codeforces.com/contest/821/problem/E 题意:我们现在位于(0,0)处,目标是走到(K,0)处.每一次我们都可以从(x,y)走到(x+1,y- ...

  7. Codeforces Round #420 (Div. 2) - E

    题目链接:http://codeforces.com/contest/821/problem/E 题意:起初在(0,0),现在要求走到(k,0),问你存在多少种走法. 其中有n条线段,每条线段为(a, ...

  8. Codeforces Round #373 (Div. 2) E. Sasha and Array 线段树维护矩阵

    E. Sasha and Array 题目连接: http://codeforces.com/contest/719/problem/E Description Sasha has an array ...

  9. Educational Codeforces Round 60 D dp + 矩阵快速幂

    https://codeforces.com/contest/1117/problem/D 题意 有n个特殊宝石(n<=1e18),每个特殊宝石可以分解成m个普通宝石(m<=100),问组 ...

随机推荐

  1. JMeter简单使用

    JMeter是apache公司基于java开发的一款开源压力测试工具.因为它是java开发的,所以运行的时候必须要安装jdk才可以:Jmeter是免安装的,所以拿到安装包后直接解压就可以使用了,它也是 ...

  2. springBoot01-helloworld-完成一个简单的RESTful API

    1.访问http://start.spring.io/ 2.选择构建工具Maven Project.Spring Boot版本 2.0.1,以及一些工程基本信息 ,最后点击Generate Proje ...

  3. 生成100个 "20180520" 这样的时间字符串 写入txt文件

    主要想记录一下 . 写NSString 到txt . 数组的去重 . 数组的截取 . 数组分割 代码如下: NSString *year = @"2018"; NSArray *m ...

  4. spring-boot整合shiro实现权限管理

    1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 2.GITHUB地址 https://github.com/nbfujx/springBo ...

  5. shell脚本学习 (8) fmt 格式化段落

    1 获取系统中的字典文件 -n隐藏查找过程  -e 匹配多次,只打印带p的行(不能写成-e -n) ,100p /usr/share/dict/words 会显示1-100行的字母 2 fmt 按默认 ...

  6. LDD3 第9章 与硬件通信

    一.I/O端口和I/O内存 每种外设都通过读写寄存器进行控制.大部分外设都有几个寄存器,不管是在内村地址空间还是在I/O地址空间,这些寄存器的访问地址都是连续的. 在硬件层,内存区域和I/O区域没有区 ...

  7. 09-排序3 Insertion or Heap Sort(25 分)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  8. sonarqube6.7.1使用

    1.插件安装 方法1.登入sonarqube-web安装 admin/admin 配置--应用市场--全部 英文片:administration--configuration--marketplace ...

  9. 2016年Esri技术公开课全年资料分享

    大家好,2016年的公开课活动在上周全部结束,感谢大家的支持. 2016年的公开课共进行20期,共有24位讲师参与,公开课视频播放.课件下载次数累计超10万次,在这里衷心的感谢大家的积极参与和分享精神 ...

  10. codeforces 559D Randomizer

    题意简述: 在一个格点图中 给定一个凸$n$边形(每个定点均在格点上),随机选择其中一些点构成一个子多边形, 求子多边形的内部点个数的期望. ----------------------------- ...