Codeforces 821E Okabe and El Psy Kongroo(Dp+矩阵快速幂)

来源:互联网 发布:淘宝怎么做话费充值 编辑:程序博客网 时间:2024/05/19 22:26

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
1 30 3 3
output
4
input
2 60 3 03 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:


大佬的题解:http://blog.csdn.net/mengxiang000000/article/details/73740695


题目大意:


我们现在位于(0,0)处,目标是走到(K,0)处。

每一次我们都可以从(x,y)走到(x+1,y-1)或者(x+1,y)或者(x+1,y+1)三个位子之一。

现在一共有N段线段,每条线段都是平行于X轴的。

我们如果此时x是在这段线段之内的话,我们此时走到的点(x,y)需要满足0<=y<=Ci.

现在保证一段线段的终点,一定是下一段线段的起点。

问我们从起点走到终点的行走方案数。


思路:


看到数据范围,很套路的Dp+矩阵快速幂。

而且这个题的Dp转移方程不难写,其实看起来这个E比D简单的多= =

假设我们此时只有一段线段且K很小,那么我们可以设定Dp【i】【j】表示走到点(i,j)的方案数。

那么很显然Dp【i】【j】+=Dp【i-1】【j】+Dp【i-1】【j-1】+Dp【i-1】【j+1】;

初始化dp【0】【0】=1即可。


然而这里K很大,那么我们对于每一段的状态转移过程使用矩阵快速幂优化一下即可。

设定矩阵的时候,我们就可以忽略掉X轴这个维度,设定dp【i】去转移即可:


过程维护上一次到达线段终点的可行方案数,然后继续转移下一段线段。

直到最终为止。


PS:其实就是根据每段的h[i],算出dp^1,矩阵幂得到dp^m,然后进行转移

#include<bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 105;const ll mod = 1e9+7;ll n, k, l[maxn], r[maxn], h[maxn];struct Matrix{    ll mat[20][20];    void init()    {        memset(mat, 0, sizeof(mat));    }}ans;Matrix mul(Matrix a, Matrix b){    Matrix ret;    ret.init();    for(int i = 1; i <= 16; i++)        for(int j = 1; j <= 16; j++)        {            ret.mat[i][j] = 0;            for(int k = 1; k <= 16; k++)            {                ret.mat[i][j] += a.mat[i][k] * b.mat[k][j];                ret.mat[i][j] %= mod;            }        }    return ret;}Matrix quick_pow(Matrix a, ll b){    Matrix ret;    ret.init();    for(int i = 1; i <= 16; i++)        ret.mat[i][i] = 1;    while(b)    {        if(b&1)            ret = mul(ret, a);        a = mul(a, a);        b >>= 1;    }    return ret;}int main(){    while(scanf("%I64d%I64d", &n, &k) != EOF)    {        ans.init();        for(int i = 1; i <= n; i++)            scanf("%I64d%I64d%I64d", &l[i], &r[i], &h[i]), h[i]++;        Matrix dp;        ans.mat[1][1] = 1;        for(int x = 1; x <= n; x++)        {            dp.init();            for(int i = 1; i <= h[x]; i++)                for(int j = 1; j <= h[x]; j++)                    if(abs(i-j) <= 1)                        dp.mat[i][j] = 1;            ll dis = r[x] - l[x];            if(x == n)                dis = k;            dp = quick_pow(dp, dis);            k -= (r[x] - l[x]);            ans = mul(dp, ans);        }        cout << ans.mat[1][1] << endl;    }    return 0;}


阅读全文
0 0
原创粉丝点击