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. PHP curl_multi_add_handle函数

    curl_multi_add_handle — 向curl批处理会话中添加单独的curl句柄 说明 int curl_multi_add_handle ( resource $mh , resourc ...

  2. VC++ 控件赋值取值

    SetWindowText(SetWindowTextW)void SetWindowText(  LPCTSTR lpszString  );GetWindowText(GetWindowTextW ...

  3. Linux Bash Shell快速入门 (三)

    forfor 循环结构与 C 语言中有所不同,在 BASH 中 for 循环的基本结构是: for $var in dostatmentsdone 其中 $var 是循环控制变量, 是 $var 需要 ...

  4. v-show与v-if的区别

    v-show有dom节点像display:none,而v-if隐藏的则没有dom节点.两个共同点都可以显隐

  5. django搭建一个小型的服务器运维网站-重启服务器的进程

    目录 项目介绍和源码: 拿来即用的bootstrap模板: 服务器SSH服务配置与python中paramiko的使用: 用户登陆与session; 最简单的实践之修改服务器时间: 查看和修改服务器配 ...

  6. php面试专题---2、常量及数据类型考点

    php面试专题---2.常量及数据类型考点 一.总结 一句话总结: 变量为null和变量判断为false的情况需要仔细注意下 1.PHP中字符串可以使用哪三种定义方法以及各自的区别是什么? 单引号:不 ...

  7. DirectX - External Overlay - 源代码

    Today i'm going to release a new version of my overlay.It's coded for beginners & users who want ...

  8. linux从head.s到start_kernelstart_kernel之---内核解压到重定位分析

    一: arm linux 内核生成过程 1. 依据arch/arm/kernel/vmlinux.lds 生成linux内核源码根目录下的vmlinux,这个vmlinux属于未压缩,带调试信息.符号 ...

  9. I2C总线协议详解

    I2C总线定义     I2C(Inter-Integrated Circuit)总线是一种由PHILIPS公司开发的两线式串行总线,用于连接微控制器及其外围设备.I2C总线产生于在80年代,最初为音 ...

  10. shell 中 比较 diff

    diff 可以用来比较文件和文件夹是否相同 比较文件 diff file1 file2 >/dev/null 比较文件夹 diff -rNaq dir1 dir2 >/dev/null - ...