Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo dp+矩阵快速幂

来源:互联网 发布:杭州proe软件速成班 编辑:程序博客网 时间:2024/05/19 22:24

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 ≤ 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 ≤ 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:



Source

Codeforces Round #420 (Div. 2)


My Solution

题意:从(0,0)走到(k,0)(1 ≤ k ≤ 1e18),每次可以从(x, y) 走到 (x+1, y+1) 或 (x+1, y) 或 (x+1, y-1),然后必须在很多个y == ci的线段下面走,

(相邻的线段,前一个的结束x坐标bi和后一个线段的开始x坐标ai+1 相同,且y = ci可能不同)


dp+矩阵快速幂

比较裸的dp+矩阵快速幂,因为这里k为1e18,所以几乎只能用矩阵快速幂来做了。

朴素的dp,dpij表示走到(i, j)时的方案数,

则 状态方程为,if(j+1 <= b[k]) dp[i+1][j+1] += dp[i][j];

                             if(j-1 >= 0) dp[i+1][j-1] += dp[i][j];

                             dp[i+1][j] += dp[i][j];

然后可以构造出15*15(ci<=15)的矩阵,把状态转移到矩阵上,然后对于每个a[k]、b[k]、c[k]跑一次快速幂即可。

此外注意a[k],b[k],以及快速幂的参数到是LL。

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


#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;typedef long long LL;const int MAX_SIZE = 100 + 8;LL a[MAX_SIZE], bb[MAX_SIZE];int c[MAX_SIZE];//n^3*log(m)const LL MOD = 1e9 + 7;const LL M_SIZE = 16;LL mod(LL &x){    x -= x / MOD * MOD;}struct Matrix{    LL m[M_SIZE][M_SIZE];    LL N; //¾ØÕóµÄ½×Êý    void init(){        memset(m, 0, sizeof m);    }    void setOne(){        init();        for(int i = 0; i < M_SIZE; i++) m[i][i] = 1;    }    Matrix(){        init();    }    Matrix operator*(const Matrix &rhs) const{        Matrix ret;        ret.N = N;        int i, j, k;        for(k = 0; k <= N; k++){            for(i = 0; i <= N; i++){                for(j = 0; j <= N; j++){                    mod(ret.m[i][j] += m[i][k] * rhs.m[k][j]);                }            }        }        return ret;    }    void print(){        for(int i = 0; i <= N; i++){            for(int j = 0; j <= N; j++)                cout << m[i][j] << " ";            cout << endl;        }        cout << endl;    }};Matrix res, b;void quickPow(LL index){    //res.print();    //b.print();    while(index){        if(index&1) res = res * b;        index >>= 1;        b = b * b;    }}char s[MAX_SIZE];int main(){    #ifdef LOCAL    freopen("e.txt", "r", stdin);    //freopen("e.out", "w", stdout);    int T = 1;    while(T--){    #endif // LOCAL    ios::sync_with_stdio(false); cin.tie(0);    LL n, kk;    cin >> n >> kk;    res.N = 15;    b.N = 15;    res.init();    res.setOne();    int i, j, k;    for(i = 1; i <= n; i++){        cin >> a[i] >> bb[i] >> c[i];    }    for(i = 1; i <= n; i++){        b.init();        for(j = 0; j <= c[i]; j++){            if(j+1 <= c[i]) b.m[j][j+1]++;            if(j-1 >=0) b.m[j][j-1]++;            b.m[j][j]++;        }        //cout << (bb[i] - a[i] + 1) << endl;        if(bb[i] >= kk) quickPow(kk - a[i]);        else quickPow(bb[i] - a[i]);    }    LL ans = res.m[0][0];    cout << ans << endl;    #ifdef LOCAL    cout << endl;    }    #endif // LOCAL    return 0;}



  Thank you!

                                                                                                                                            ------from ProLights

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