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. Java IO流总结(二)-示例

    1.使用文件操作的9大方法完成文件的判断 判断文件或目录是否存在 : exits() 返回值boolean型 * 判断是否是文件: isFile() boolean * 判断是否是目录: isDiec ...

  2. Python3解leetcode Factorial Trailing Zeroes

    问题描述: Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 ...

  3. 【Linux】grub引导修复

    将系统盘挂载上并设置开机从光盘启动(U盘也可以) 进入系统安装引导初始界面,然后选择最后一项Troubleshooting 然后选择第二项Rescue a CentOS system进入系统救援模式选 ...

  4. LUOGU P4783 【模板】矩阵求逆(高斯消元)

    传送门 解题思路 用高斯消元对矩阵求逆,设\(A*B=C\),\(C\)为单位矩阵,则\(B\)为\(A\)的逆矩阵.做法是把\(B\)先设成单位矩阵,然后对\(A\)做高斯消元的过程,对\(B\)进 ...

  5. 2018-2019-2 《Java程序设计》第11周学习总结

    20175319 2018-2019-2 <Java程序设计>第11周学习总结 教材学习内容总结 本周学习<Java程序设计>第十三章java网络编程: - URL类 URL类 ...

  6. outlook使用inline style回复邮件

    Reply with inline comments within the original message text When you reply to email messages in Outl ...

  7. Configuring IPMI under Linux using ipmitool

    http://www.thomas-krenn.com/en/wiki/Configuring_IPMI_under_Linux_using_ipmitool Configuring IPMI und ...

  8. smartGit的使用

    合理管理自己的代码,对程序员来说是一件很重要的事,今天我也走上了github托管之路 要在Git上托管代码,首先你要要github官网创建一个代码仓库,用来放你的代码. 一,要托管到github,那你 ...

  9. 在线清空nohup.out内容

    通过 cat /dev/null > filename 命令可以在线清空nohup.out里的内容

  10. PAT_A1077#Kuchiguse

    Source: PAT A1077 Kuchiguse (20 分) Description: The Japanese language is notorious for its sentence ...