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 = 0an ≤ 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 xvalue 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 ≤ 1001 ≤ 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 ≤ 10180 ≤ ci ≤ 15) — the left and right ends of a segment, and its y coordinate.

It is guaranteed that a1 = 0an ≤ 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
Copy
1 3
0 3 3
output
4
input
Copy
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),走的过程必须在线的下面,问有多少种走法?

思路:状态转移方程 dp[i][j]=dp[i-1][j-1]+dp[i-1][j]+dp[i-1][j+1]转移,但是发现转移次数达到1e18必定会TLE.那么能不能有方法加个LOG大大降低复杂度呢? 有,用矩阵快速幂.

复杂度为O(n*log(k)*15^3)

用矩阵快速幂很典型的数据是,转移次数非常的多,但每一次转移的状态少,因为假设当前要转移n个状态,那么矩阵快速幂的复杂度就到来n^3*log(k)的复杂度,因为k非常的大,所以n一定会比较小,这样才可以用矩阵快速幂来优化.看到1e18数据的DP要考虑是否每次转移的状态少,且总共转移状态的次数异常的多,那么需要考虑用矩阵快速幂来log掉转移的总次数.

第二次做这个题,理解又加深了,开森!

#include <bits/stdc++.h>
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define bug cout << "bug" << endl
#pragma comment(linker, "/STACK:102400000,102400000")


using namespace std;
typedef long long ll;

const int MAX_N=1e5+5;
typedef struct{
    ll mat[20][20];
}matrix;

matrix matrix_mul(matrix a,matrix b,ll len){
    matrix c;
    memset(c.mat,0,sizeof(c.mat));
    for(int i=0;i<=len;i++){
        for(int j=0;j<=len;j++){
            for(int k=0;k<=len;k++){
                c.mat[i][j]+=(a.mat[i][k]%MOD*b.mat[k][j]%MOD);
                c.mat[i][j]%=MOD;
            }
        }
    }
    return c;
}

matrix matrix_qucick_pow(matrix a,ll k,ll len){
    matrix c;
    memset(c.mat,0,sizeof(c.mat));
    for(int i=0;i<=15;i++)  c.mat[i][i]=1;
    while(k){
        if(k&1) c=matrix_mul(c,a,len);
        a=matrix_mul(a,a,len);
        k>>=1;
    }
    return c;
}

int main(void){
    ll n,k;
    cin >> n>>k;
    matrix temp,a,b;
    memset(a.mat,0,sizeof(a.mat));
    memset(b.mat,0,sizeof(b.mat));
    memset(temp.mat,0,sizeof(temp.mat));
    temp.mat[0][0]=temp.mat[0][1]=1;
    for(int i=1;i<=14;i++){
        for(int j=i-1;j<=i+1;++j)
            temp.mat[i][j]=1;
    }
    temp.mat[15][14]=temp.mat[15][15]=1,a.mat[0][0]=1;
//    for(int i=0;i<=15;i++){
//        for(int j=0;j<=15;j++)  printf("%d",temp.mat[i][j]);puts("");
//    }
    for(int i=1;i<=n;i++){
        ll l,r,y;
        scanf("%lld%lld%lld",&l,&r,&y);
        if(r>k) r=k;
        for(int i=y+1;i<=15;++i)    a.mat[i][0]=0;//删去不可能的情况,不对后面的状态转移造成影响
        b=matrix_qucick_pow(temp,r-l,y);
        a=matrix_mul(b,a,y);
    }
    cout << a.mat[0][0]%MOD<< endl;
}