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. xmpp 登录注册小结

    将XMPPStream放在APPDelegate,以便全局访问 #pragma mark - XMPP相关的属性和方法定义 /** * 全局xmppstream,只读属性 */ @property ( ...

  2. 【bzoj4889】[Tjoi2017]不勤劳的图书管理员 树状数组+分块+二分

    题目描述(转自洛谷) 加里敦大学有个帝国图书馆,小豆是图书馆阅览室的一个书籍管理员.他的任务是把书排成有序的,所以无序的书让他产生厌烦,两本乱序的书会让小豆产生这两本书页数的和的厌烦度.现在有n本被打 ...

  3. 【容斥】HDU 4135 Co-prime

    acm.hdu.edu.cn/showproblem.php?pid=4135 [题意] 询问[a,b]中与n互质的数有多少个 [思路] 考虑[1,m]中与n互质的数有多少个,答案就是query(b) ...

  4. BZOJ 1006 [HNOI2008]神奇的国度==最大势算法

    神奇的国度 K国是一个热衷三角形的国度,连人的交往也只喜欢三角原则.他们认为三角关系:即AB相互认识,BC相互认识,CA相互认识,是简洁高效的.为了巩固三角关系,K国禁止四边关系,五边关系等等的存在. ...

  5. 【angularjs学习】简单的语法

    <div ng-app="" ng-init="names=[{name:'Jani',country:'Norway'},{name:'Hege',country ...

  6. spring中quartz的使用。【转http://www.cnblogs.com/kay/archive/2007/11/02/947372.html】

    注:从spring3到spring4改变 org.springframework.scheduling.quartz.CronTriggerBean org.springframework.sched ...

  7. android开发里遇到的坑——eclipse项目导入android studio以后Run按钮灰色

    android studio编译有错误但是没有提示给用户,关闭android studio重新启动项目后,会显示错误,修复以后即可!

  8. linux下面MySQL变量修改及生效

    今天在访问mysql项目的时候突然报500错误,没有找到连接,因此想到mysql的连接时间. mysql> show global variables; 主要就是连接时间是28800(8小时), ...

  9. codeforces 1041 e 构造

    Codeforces 1041 E 构造题. 给出一种操作,对于一棵树,去掉它的一条边.那么这颗树被分成两个部分,两个部分的分别的最大值就是这次操作的答案. 现在给出一棵树所有操作的结果,问能不能构造 ...

  10. CF 2018 Battle of Brains GYM 102062 F

    https://codeforces.com/gym/102062/attachments/download/8213/2018-battle-of-brains-en.pdf https://cod ...