E. Okabe and El Psy Kongroo
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 ≤ ci when 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 x coordinate.

The next n lines contain three space-separated integers ai, bi, 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:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<bitset>
#include<time.h>
using namespace std;
#define LL long long
#define pi (4*atan(1.0))
#define eps 1e-4
#define bug(x) cout<<"bug"<<x<<endl;
const int N=3e5+,M=4e6+,inf=,mod=1e9+;
const LL INF=1e18+,MOD=1e9+; struct Matrix
{
LL a[][];
Matrix()
{
memset(a,,sizeof(a));
}
void init()
{
for(int i=;i<;i++)
for(int j=;j<;j++)
a[i][j]=(i==j);
}
Matrix operator + (const Matrix &B)const
{
Matrix C;
for(int i=;i<;i++)
for(int j=;j<;j++)
C.a[i][j]=(a[i][j]+B.a[i][j])%MOD;
return C;
}
Matrix operator * (const Matrix &B)const
{
Matrix C;
for(int i=;i<;i++)
for(int k=;k<;k++)
for(int j=;j<;j++)
C.a[i][j]=(C.a[i][j]+1LL*a[i][k]*B.a[k][j])%MOD;
return C;
}
Matrix operator ^ (const LL &t)const
{
Matrix A=(*this),res;
res.init();
LL p=t;
while(p)
{
if(p&)res=res*A;
A=A*A;
p>>=;
}
return res;
}
};
map<pair<LL,int> ,LL >dp;
LL a[N],b[N];int c[N];
Matrix Gbase(int n)
{
Matrix a;
a.init();
for(int i=;i<=n;i++)
{
if(i->=)a.a[i-][i]=;
a.a[i][i]=;
if(i+<=n)a.a[i+][i]=;
}
return a;
}
Matrix Gpre(LL x,int n)
{
Matrix a;
a.init();
for(int i=;i<=n;i++)
a.a[][i]=dp[make_pair(x,i)];
return a;
}
int main()
{
int n;
LL k;
scanf("%d%lld",&n,&k);
for(int i=;i<=n;i++)
scanf("%lld%lld%d",&a[i],&b[i],&c[i]);
dp[make_pair(,)]=;
for(int i=;i<=n;i++)
{
Matrix base=Gbase(c[i]);
Matrix pre=Gpre(a[i],c[i]);
LL l=a[i],r=min(b[i],k);
base=base^(r-l);
Matrix ans=pre*base;
for(int j=;j<=c[i];j++)
dp[make_pair(r,j)]=ans.a[][j];
if(b[i]>=k)break;
}
printf("%lld\n",dp[make_pair(k,)]);
return ;
}

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   Okabe likes to take walks but knows that spies from the Organization c ...

  3. CF821 E. Okabe and El Psy Kongroo 矩阵快速幂

    LINK 题意:给出$n$条平行于x轴的线段,终点$k$坐标$(k <= 10^{18})$,现在可以在线段之间进行移动,但不能超出两条线段的y坐标所夹范围,问到达终点有几种方案. 思路:刚开始 ...

  4. Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations (矩阵高速幂)

    题目地址:http://codeforces.com/contest/551/problem/D 分析下公式能够知道,相当于每一位上放0或者1使得最后成为0或者1.假设最后是0的话,那么全部相邻位一定 ...

  5. Codeforces Round #189 (Div. 1) C - Kalila and Dimna in the Logging Industry 斜率优化dp

    C - Kalila and Dimna in the Logging Industry 很容易能得到状态转移方程 dp[ i ] = min( dp[ j ] + b[ j ] * a[ i ] ) ...

  6. Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations 矩阵快速幂优化dp

    D. GukiZ and Binary Operations time limit per test 1 second memory limit per test 256 megabytes inpu ...

  7. Codeforces Round #420 (Div. 2)

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

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

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

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

随机推荐

  1. vue-router基本使用

    路由,其实就是指向的意思,当我点击页面上的home按钮时,页面中就要显示home的内容,如果点击页面上的about 按钮,页面中就要显示about 的内容.Home按钮  => home 内容, ...

  2. Django框架----在Python脚本中调用Django环境

    在项目根目录下新建脚本文件script.py import os if __name__ == '__main__': os.environ.setdefault("DJANGO_SETTI ...

  3. 同行span标签设置display:inline-block;overflow:hidden垂直对齐问题

    1 问题描述:一个div包含 三个span 当span2 类样式设置如下图时,将导致垂直方向不对齐的情况 2解决方案: 将前面的也设置同样的样式 overflow:hidden; display:in ...

  4. 爬虫之牛掰的scrapy框架

    一. Scrapy简介及安装 http://python.jobbole.com/86405/ Scrapy的详细介绍   1.简介   2.安装     1.window上安装:         先 ...

  5. dropout——gluon

    https://blog.csdn.net/lizzy05/article/details/80162060 from mxnet import nd def dropout(X, drop_prob ...

  6. JavaScript笔记 #08# 用Regex辅助生成文章目录 V2.0

    索引 简介 测试用例 代码 简介 * 用Regex辅助生成文章目录 2.0 * 1.提高了功能的通用性(假定的文章格式更加普遍,即按照h2h3h4分级) * 2.改善了代码的可读性(稍微牺牲了一点点性 ...

  7. airtest 记录

    from airtest.core.api import * # 通过ADB连接本地Android设备 connect_device("Android:///") #安装待测软件a ...

  8. mysql 通过查看mysql 配置参数、状态来优化你的mysql

    我把MYISAM改成了INNODB,数据库对CPU方面的占用变小很多' mysql的监控方法大致分为两类: 1.连接到mysql数据库内部,使用show status,show variables,f ...

  9. enq: TM - contention一例

    今天下午,有台服务器出现异常,响应特别慢,io等待奇高,awr top 5事件如下: 经回查ash,找到了造成这些事件的sql语句,如下: select * from v$active_session ...

  10. javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 解决方法

    在使用hibernate validator进行参数有效性校验的时候,我们有时候会遇到javax.validation.UnexpectedTypeException: HV000030: No va ...