E. Okabe and El Psy Kongroo(递推+矩阵快速幂)

12 篇文章 0 订阅
2 篇文章 0 订阅
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, thei-th of which goes from x = ai to x = bi inclusive, and is aty = ci. It is guaranteed thata1 = 0, an ≤ k ≤ bn, andai = bi - 1 for2 ≤ i ≤ n. The i-th line segment forces Okabe to walk with y-value in the range 0 ≤ y ≤ ci when hisx 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 andk (1 ≤ n ≤ 100,1 ≤ k ≤ 1018) — the number of segments and the destinationx coordinate.

The next n lines contain three space-separated integersai,bi, andci (0 ≤ ai < bi ≤ 1018,0 ≤ ci ≤ 15) — the left and right ends of a segment, and itsy coordinate.

It is guaranteed that a1 = 0,an ≤ k ≤ bn, andai = bi - 1 for2 ≤ 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:


cf的div2的最后一道题,当时想得出来是个dp,但是如果是逐个x这样推过去肯定是超时的(1e18),然后就不会做了,没用过矩阵快速幂。

之后搜索了一下题解,大概是这么个意思。

首先你要一段一段的优化。用矩阵吧dp[x][y]=dp[x-1][y]+dp[x-1][y-1]+dp[x+1][y+1]这个关系表达出来。

我们注意到c的最大值只有16,那么与dp[x][16]有关系的就是dp[x-1][16],dp[x-1][15]。与dp[x][15]有关系的就是dp[x-1][15],dp[x-1][16],dp[x-1][14];以此类推,这样的话我们可以忽略x,因为我们下面要用矩阵来表示了;

我们这样写一个矩阵 [dp[0],dp[1],dp[2],dp[3]--------dp[16]],然后我们再写一个17*17的一个矩阵来表示他们的递推关系。

17*17矩阵如下:

1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1

两个矩阵相乘就等于把dp矩阵向前移动了1

对于每一段来说,每次移动段长度次-1就可以把dp矩阵推到当前的末位置坐标上去,并且用给出的c来控制矩阵大小,因为不能超过c嘛;

写成了矩阵就可以用矩阵快速幂来实现了。

#include<bits/stdc++.h>
using namespace std;
const long long mod=1000000007;
struct aa
{
    long long m[17][17];
};
aa mul(aa a,aa b,int n)
{
    aa ans;
    memset(ans.m,0,sizeof(ans.m));
    for(int i=0; i<=n; i++)
    {
        for(int j=0; j<=n; j++)
        {
            for(int k=0; k<=n; k++)
            {
                ans.m[i][j]+=((a.m[i][k]%mod)*(b.m[k][j]%mod));
                ans.m[i][j]%=mod;
            }
        }
    }
    return ans;
}
aa fastmod(aa a,long long time,int n)
{
    aa ans;
    memset(ans.m,0,sizeof(ans.m));
    for(int i=0; i<=n; i++)
        ans.m[i][i]=1;
    while(time)
    {
        if(time&1)
        {
            ans=mul(ans,a,n);
        }
        time=time>>1;
        a=mul(a,a,n);
    }
    return ans;
}
int main()
{
    int n;
    long long k;
    aa A;
    memset(A.m,0,sizeof(A.m));
    for(int i=0; i<=16; i++)
    {
        for(int j=i-1; j<=i+1; j++)
        {
            if(j>=0&&j<=16)
            {
                A.m[i][j]=1;
            }
        }
    }
    while(scanf("%d%lld",&n,&k)!=EOF)
    {
        long long dp1[17],dp2[17];
        memset(dp1,0,sizeof(dp1));
        memset(dp2,0,sizeof(dp2));
        dp2[0]=1;
        for(int i=0; i<n; i++)
        {
            long long a,b,c;
            scanf("%lld%lld%lld",&a,&b,&c);
            if(i==n-1)
                b=k;
            memset(dp1,0,sizeof(dp1));
            for(int i1=0; i1<=c; i1++)
            {
                dp1[i1]=dp2[i1];
            }
            memset(dp2,0,sizeof(dp2));
            aa ans=fastmod(A,b-a,c);
            for(int i1=0; i1<=c; i1++)
            {
                for(int j=0; j<=c; j++)
                {
                    dp2[i1]+=((dp1[j]%mod)*(ans.m[i1][j]%mod));
                    dp2[i1]%=mod;
                }

            }
        }
        printf("%lld\n",dp2[0]);
    }

}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值