E - Okabe and El Psy Kongroo CodeForces - 821E

安全路径计数
本文介绍了一个关于计算从原点到指定点的安全路径数量的问题。在考虑了特定限制条件下的路径规划,包括避免被监视区域的同时,利用矩阵快速幂的方法进行高效求解。

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

Example

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:

裸的矩阵快速幂的题目,练了一练,快速幂用倍增写,不然容易萎。

using namespace std;
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define ll long long

struct matrix{
    ll it[21][21];
}mu;
const ll mod=1e9+7;
ll n,k;
ll a[201],b[201],c[201];
ll ans[21];
ll bef[21];
matrix st[65];
void prll(matrix xx)
{
    for(ll i=0;i<=20;i++)
    {
        for(ll j=0;j<=20;j++)
        {
            cout<<xx.it[i][j]<<" ";
        }
        puts("");
    }
}
matrix operator * (matrix a,matrix b)
{
    matrix c;
    for(ll i=0;i<=20;i++)
    {
        for(ll j=0;j<=20;j++)
        {
            c.it[i][j]=0;
            for(ll k=0;k<=20;k++)
            {
                c.it[i][j]=(c.it[i][j]+a.it[i][k]*b.it[k][j])%mod;
            }
        }
    }
    return c;
}
matrix pow(ll x)
{
    matrix hh;
    bool flag=0;
    st[1]=mu;
    ll cnt=0;
    for(ll i=2;i<=64;i++)st[i]=st[i-1]*st[i-1];
    while(x)
    {
        cnt++;
        if(x%2){if(!flag) hh=st[cnt];else hh=hh*st[cnt];flag=1;}
        x>>=1;
    }
    return hh;
}
ll tot;
int main()
{
    cin>>n>>k;
    ans[0]=1;
    for(ll i=1;i<=n;i++)
    {
        cin>>a[i]>>b[i]>>c[i];
        if(b[i]>=k)
        {
            b[i]=k;
            tot=i;break;
        }
    }
    n=tot;
    for(ll i=1;i<=n;i++)
    {
        memset(mu.it,0,sizeof(mu.it));
        for(ll j=0;j<=c[i];j++)
        {
            if(j==0){mu.it[j][0]=mu.it[j][1]=1;}
            else
            if(j==c[i])
            {
                mu.it[j][c[i]]=mu.it[j][c[i]-1]=1;
            }
            else
            mu.it[j][j-1]=mu.it[j][j]=mu.it[j][j+1]=1;
        }
        matrix hh=pow(b[i]-a[i]);
        for(ll j=0;j<=20;j++)
        {
            bef[j]=0;
            for(ll k=0;k<=20;k++)
            {   
                bef[j]=(bef[j]+ans[k]*hh.it[k][j])%mod;
            }
        }
        for(ll j=0;j<=20;j++) ans[j]=bef[j];
        for(ll j=c[i]+1;j<=20;j++) ans[j]=0;
    }
    cout<<ans[0]<<endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值