E. Okabe and El Psy Kongroo
 
 

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

题意:

  给你一个起点(0,0),和终点(k,0)

  假设现在在(x,y),下一步你可以走到(x+1,y)、(x+1,y-1)、(x+1,y+1);

  但是不能超过给定的上界线段和正x轴,也就是每一步都要在这两个线段中间

  问你有多少种走法,走到终点

题解:

  C很小,只有15

  每个点向左边走一步,就是, dp[x][y]==》dp[x+1][y]、dp[x+1][y+1]、dp[x+1][y-1],

  x最多走10^18步,y最多15,用矩阵快速幂加速求解这个dp方程

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double Pi = acos(-1.0);
const int N = 1e4+, M = 1e3+, inf = 2e9;
LL mod = 1e9+; LL a[N],b[N];
int c[N],n;
struct Matix {
LL arr[][];
}E,first,dp[];
Matix mul(Matix a,Matix b,LL hang ,LL lie) {
Matix ans;
memset(ans.arr,,sizeof(ans.arr));
for(int i=;i<=hang;i++) {
for(int t=;t<=lie;t++)
for(int j=;j<=lie;j++) {
ans.arr[i][t]+=(a.arr[i][j]*b.arr[j][t])%mod,
ans.arr[i][t]%=mod;
}
}
return ans;
} Matix multi (Matix a, Matix b,int hang,int lie,int lie2) {
Matix ans;
memset(ans.arr,,sizeof(ans.arr));
for(int i = ; i < hang; i++) {
for(int j = ; j < lie2; j++) {
ans.arr[i][j] = ;
for(int k = ; k < lie; k++)
ans.arr[i][j] += (a.arr[i][k] * b.arr[k][j])%mod,
ans.arr[i][j] %= mod;
}
}
return ans;
} Matix pow(Matix ans,Matix a,LL x,int cc) {
while(x) {
if(x&) ans=multi(ans,a,,cc+,cc+);
a=mul(a,a,cc,cc);
x/=;
}
return ans;
}
LL K;
int main() {
scanf("%d%lld",&n,&K);
for(int i = ; i <= n; ++i) {
scanf("%lld%lld%d",&a[i],&b[i],&c[i]);
}
dp[].arr[][] = ;
for(int i = ; i <= n; ++i) {
memset(first.arr,,sizeof(first.arr));
for(int j = ; j <= c[i]; ++j) first.arr[][j] = dp[i-].arr[][j];
memset(E.arr,,sizeof(E.arr));
int sum = ;
for(int j = ; j <= c[i]; ++j) {
if(sum) E.arr[][j] = ,sum--;
}
int fir = ;
for(int j = ; j <= c[i]; ++j) {
for(int k = fir; k <= min(fir+,c[i]); ++k) {
E.arr[j][k] = ;
}
fir++;
}
dp[i] = pow(first,E,min(b[i],K) - a[i],c[i]);
// dp[i] = multi(first,E,1,c[i]+1,c[i]+1);
}
printf("%lld\n",(dp[n].arr[][]+mod)%mod);
return ;
}
 

Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo DP+矩阵快速幂加速的更多相关文章

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

    E. Okabe and El Psy Kongroo   Okabe likes to take walks but knows that spies from the Organization c ...

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

    E. Okabe and El Psy Kongroo time limit per test 2 seconds memory limit per test 256 megabytes input ...

  3. Codeforces 821E Okabe and El Psy Kongroo(矩阵快速幂)

    E. Okabe and El Psy Kongroo time limit per test 2 seconds memory limit per test 256 megabytes input ...

  4. Codeforces Round #420 (Div. 2)

    /*************************************************************************************************** ...

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

    本来打算划划水洗洗睡了,突然听到这次的主人公是冈部伦太郎 石头门(<steins;gate>)主题的比赛,岂有不打之理! 石头门真的很棒啊!人设也好剧情也赞曲子也特别好听. 推荐http: ...

  6. codeforces E. Okabe and El Psy Kongroo(dp+矩阵快速幂)

    题目链接:http://codeforces.com/contest/821/problem/E 题意:我们现在位于(0,0)处,目标是走到(K,0)处.每一次我们都可以从(x,y)走到(x+1,y- ...

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

    题目链接:http://codeforces.com/contest/821/problem/E 题意:起初在(0,0),现在要求走到(k,0),问你存在多少种走法. 其中有n条线段,每条线段为(a, ...

  8. Codeforces Round #373 (Div. 2) E. Sasha and Array 线段树维护矩阵

    E. Sasha and Array 题目连接: http://codeforces.com/contest/719/problem/E Description Sasha has an array ...

  9. Educational Codeforces Round 60 D dp + 矩阵快速幂

    https://codeforces.com/contest/1117/problem/D 题意 有n个特殊宝石(n<=1e18),每个特殊宝石可以分解成m个普通宝石(m<=100),问组 ...

随机推荐

  1. [android开发篇]自定义权限

    有时候,我们可能遇到如下需求场景:当用户在一个应用程序中进行某项操作时,会启动另外一个应用程序,最常见的时直接打开了另外一个应用程序,并进入其中某个Activity(如:有的应用中有推荐应用列表,当用 ...

  2. .NET重构(三):在注册和充值中,触发器的使用

    导读:机房做到注册和充值了,有两个关键点:在注册的时候,同时给该用户写入充值记录:在充值的时候,给该用户更改余额信息.第一次做的时候,是一条一条的写,那时候师傅就说了触发器和存储过程的使用,现在终于用 ...

  3. oracle客户端安装与配置

    在进行开发时经常需要连接Oracle数据库,一般的场景是Oracle数据库在远程服务器上,本地计算机通过plsql developer来访问. 这就要求在本地安装好plsql developer,但是 ...

  4. [UOJ#122][NOI2013]树的计数

    [UOJ#122][NOI2013]树的计数 试题描述 我们知道一棵有根树可以进行深度优先遍历(DFS)以及广度优先遍历(BFS)来生成这棵树的 DFS 序以及 BFS 序.两棵不同的树的 DFS 序 ...

  5. Hibernate get 和 load区别

    Session.load/get方法均可以根据指定的实体类和id从数据库读取记录,并返回与之对应的实体对象.下边详细说一下get和load的不同,因为有些时候为了对比也会把find加进来. 1.从返回 ...

  6. java面试题之hashcode相等两个类一定相等吗?equals呢?相反呢?

    答:hashcode相等,两个类不一定相等,equals也不一定相等: 反过来,equals相等,hashcode一定相等

  7. Fruit Ninja

    Fruit Ninja 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C++ 262144K,其他语言524288K 64bit IO Format: %lld 题目描述 Fruit Ni ...

  8. Laravel 之Composer

    Composer是php的依赖管理工具,不是包管理器. 安装方式: (1)Composer-Setup.exe Win操作系统.需FQ (2)composer.phar 通用安装方式.不需要FQ,wi ...

  9. Louvain algorithm for community detection

    主要理解Louvain 算法中对于模块度的定义:模块度是评估一个社区网络划分好坏的度量方法,它的物理含义是社区内节点的连边数与随机情况下的边数只差,它的取值范围是 [−1/2,1).可以简单地理解为社 ...

  10. Java的常用对象

    PO:持久对象 (persistent object),po(persistent object)就是在Object/Relation Mapping框架中的Entity,po的每个属性基本上都对应数 ...