Codeforce821E Okabe and El Psy Kongroo

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 = 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 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 ≤ 100, 1 ≤ 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 ≤ 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).

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:

题意:给n条水平线段点终点高度,只能右上方向走,向右走,右下方向走,只能在第一象限及坐标轴上线段下面运动问到(K,0)有多少种方案

题解:可以推出DP式子,对每一条线段使用快速幂

#include <bits/stdc++.h>
#define maxn 20
using namespace std;
long long mod = 1e9+7, len=20, n, k, sta[110], en[110], c[110];
struct mat{
    long long m[maxn][maxn];//注意初始化
    mat friend operator*(mat a,mat b){
        mat d;
        memset(d.m, 0, sizeof(d.m));
        for(int i=0;i<len;i++)
            for(int j=0;j<len;j++)
                for(int k=0;k<len;k++)
                d.m[i][j] = (d.m[i][j]%mod+(a.m[i][k]*b.m[k][j])%mod)%mod;
        return d;
    }
};
mat f(mat x,long long num){
    mat t;
    memset(t.m, 0, sizeof(t.m));
    for(int i=0;i<len;i++) t.m[i][i] = 1;
    while(num){
        if(num&1) t = t*x;
        x = x*x;
        num >>= 1;
    }
    return t;
}
int main(){
    int i,j;
    mat s, ss;
    memset(s.m, 0, sizeof(s.m));
    memset(ss.m, 0, sizeof(ss.m));
    s.m[0][0] = 1;
    for(i=0;i<len;i++)
        for(j=i-1;j<=i+1;j++)
            if(j>=0&&j<len)
                ss.m[i][j] = 1;
    cin>>n>>k;
       for(i=0;i<n;i++) cin>>sta[i]>>en[i]>>c[i];
       i = 0;
    while(i<n&&k>=en[i]){
           len = c[i]+1;
        s = s*f(ss, en[i]-sta[i]);
           i++;
    }
    len = c[i]+1;
    s = s*f(ss, k-en[i-1]);
    cout<<s.m[0][0]<<endl;
    return 0;
}
时间: 2024-08-27 22:07:08

Codeforce821E Okabe and El Psy Kongroo的相关文章

Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo(矩阵)

题目链接:Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo 题意: 在一个二维方格子里有n条线段,有三种走法 (x?+?1,?y?+?1), (x?+?1,?y), or (x?+?1,?y?-?1). 现在要求每次都要在线段下行走,问你有多少种走法, 可以从(0,0)到(k,0). 题解: 考虑dp f[i][j]=f[i-1][j]+f[i-1][j+1]+f[i-1][j-1]. 由于k比较大c比较小,可以考虑用矩阵来优化

CF821 E. Okabe and El Psy Kongroo 矩阵快速幂

LINK 题意:给出$n$条平行于x轴的线段,终点$k$坐标$(k <= 10^{18})$,现在可以在线段之间进行移动,但不能超出两条线段的y坐标所夹范围,问到达终点有几种方案. 思路:刚开始以为限制只是到达线段上就必须沿线段走,后来才发现是要求走y坐标所夹范围,那么就简单多了,很容易看出是个递推形DP,然而数据量有点大,k为10的18次,一般转移显然不可行.由于是个递推,而且y坐标最大也只有15,故使用矩阵优化递推复杂度即可. /** @Date : 2017-07-04 16:06:18

cf 821E Okabe and El Psy Kongroo(矩阵快速幂)

链接:http://codeforces.com/problemset/problem/821/E 分析:由于有边界而且不同段边界还不同,直接算是不行的..k是1e18,dp也不行..用一个16维的向量表示某一列16个位置可能的种类数,到下一列的转移矩阵容易得到,而且在同一段里转移矩阵一样,直接用快速幂算出这一段结束的向量,然后继续推下一段结束的向量.注意特殊情况的处理. 1 #include<iostream> 2 #include<cstring> 3 using namesp

[codeforces821E]Okabe and El Psy Kongroo

题意:(0,0)走到(k,0),每一部分有一条线段作为上界,求方案数. 解题关键:dp+矩阵快速幂,盗个图,注意ll 1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 #include<algorithm> 5 #include<cmath> 6 #include<iostream> 7 using namespace std; 8 typedef long lo

Codeforces 821E Okabe and El Psy Kongroo

题意:我们现在位于(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[i][j]+=dp[i-1][j]+dp[-1][j-1]+dp[i-1][j+1] 之后构造一

Codeforces Round #420 (Div. 2) A-E

本来打算划划水洗洗睡了,突然听到这次的主人公是冈部伦太郎 石头门(<steins;gate>)主题的比赛,岂有不打之理! 石头门真的很棒啊!人设也好剧情也赞曲子也特别好听. 推荐http://music.163.com/#/m/song?id=26259014&userid=115264555 (强行跑题) Okabe and Future Gadget Laboratory O(n^4)暴力妥妥的 1 #include<iostream> 2 #include<al

Codeforces Round #420 (Div. 2)

/****************************************************************************************************************** 因为发现不敲题会变蠢...所以没事还是做两道题吧.... 很久没做过题然后发现C和E全都是因为没用LL给卡住了......  我真的是太蠢了 ***************************************************************

Codeforces Round #420 E

Okabe and El Psy Kongroo 题意:有n条平行x轴的线段,每条线段的起点为(ai,ci),终点为(bi,ci),且满足ai=b(i-1),从起点出发,每次可以往3个方向走,分别为 (x?+?1,?y?+?1), (x?+?1,?y), or (x?+?1,?y?-?1),且走的过程必须满足0>=y>=ci,求走到(k,0)有多少种走法 思路:dp+矩阵快速幂, dp[i][0]=dp[i-1][0]+dp[i-1][1] ........... dp[i][j]=dp[i-

0ops CTF/0CTF writeup

0×00 0CTF『第一届0ops信息安全技术挑战赛,即0ops Capture The Flag,以下简称0CTF. 0CTF由上海交通大学网络信息中心和上海市信息安全行业协会指导,由上海交通大学信息网络安全协会承办,是2014信息安全技能竞赛校园赛首站.0CTF注册与参赛地址为http://ctf.0ops.net. 比赛时间为北京时间2014年3月1日至2日,每天7时至23时,共32小时.』 看官方微博,这个比赛本来是面向上交校内的,就是校外可以做题但是不发奖,后来也给校外发奖了,整体感觉