传送门

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

input
2 6
0 3 0
3 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))

//china no.1
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
using namespace std;#define pi acos(-1)
#define endl '\n'
#define rand() srand(time(0));
#define me(x) memset(x,0,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0); cin.tie(0);
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,-1,-1,1,1};
const int dy[]={0,1,0,-1,1,-1,1,-1};
const int maxn=1e3+5;
const int maxx=1e5+100;
const double EPS=1e-7;
const int MOD=10000007;
#define mod(x) ((x)%MOD);
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
//typedef tree<pt,null_type,less< pt >,rb_tree_tag,tree_order_statistics_node_update> rbtree;
long long gcd(long long a , long long b){if(b==0) return a;a%=b;return gcd(b,a);}
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define W whileinline int Scan()
{int res=0,ch,flag=0;if((ch=getchar())=='-')flag=1;else if(ch>='0' && ch<='9')res=ch-'0';while((ch=getchar())>='0'&&ch<='9')res=res*10+ch-'0';return flag ? -res : res;
}LL a[maxx],bb[maxx],c[maxx],n,k,mod=1e9+7,t;
int ssize = 15;struct Matrix
{LL  m[25][25];void init(){memset(m, 0, sizeof m);}void setOne(){init();for(int i = 0; i <=ssize; i++) m[i][i] = 1;}void print(){for(int i = 0; i <= ssize; i++){for(int j = 0; j <= ssize; j++)cout << m[i][j] << " ";cout << endl;}cout << endl;}} I,A,B,T,b,res;Matrix Mul(Matrix a,Matrix b)  //
{int i,j,k;Matrix c;for (i = 0; i <= ssize; i++){for(j = 0; j <= ssize; j++){c.m[i][j]=0;for(k = 0; k <= ssize; k++){c.m[i][j]+=(a.m[i][k]*b.m[k][j]);c.m[i][j]%=mod;}}}return c;
}void quickPow(LL n)
{while(n){if(n&1) res=Mul(res,b);n>>=1;b=Mul(b,b);}
}
int main()
{close();cin>>t>>k;FOR(1,t,i)cin>>a[i]>>bb[i]>>c[i];res.init();res.setOne();FOR(1,t,i){b.init();for(int 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]++;}//b.print();// cout<<"**********"<<endl;// res.print();if(bb[i] >= k) quickPow(k - a[i]);else quickPow(bb[i] - a[i]);}LL ans = res.m[0][0];cout << ans << endl;
}

E.Okabe and El Psy Kongroo相关推荐

  1. codeforce#420 E. Okabe and El Psy Kongroo(图论+矩阵快速幂)

    题目链接 E. Okabe and El Psy Kongroo 分析 首先对于坐标为 (x,y)(x,y) 的点,设 f(x,y)f(x,y) 表示从原点到 (x,y)(x,y) 的路径不难得到递推 ...

  2. Codeforce821E Okabe and El Psy Kongroo

    题意:给n条水平线段点终点高度,只能右上方向走,向右走,右下方向走,只能在第一象限及坐标轴上线段下面运动问到(K,0)有多少种方案 题解:可以推出DP式子,对每一条线段使用快速幂 #include & ...

  3. 【codeforces 821E】Okabe and El Psy Kongroo

    [题目链接]:http://codeforces.com/problemset/problem/821/E [题意] 一开始位于(0,0)的位置; 然后你每次可以往右上,右,右下3走一步; (x+1, ...

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

    LINK 题意:给出$n$条平行于x轴的线段,终点$k$坐标$(k <= 10^{18})$,现在可以在线段之间进行移动,但不能超出两条线段的y坐标所夹范围,问到达终点有几种方案. 思路:刚开始 ...

  5. [CF 821E] Okabe and El Psy Kongroo

    题目 洛谷 题意 给定一个二维坐标系和一些线段,求从 (0,0)(0,0)(0,0) 走到 (k,0)(k,0)(k,0) 的方案总数.若当前坐标为 (x,y)(x,y)(x,y),下一步可以拓展到 ...

  6. CF821E Okabe and El Psy Kongroo

    一.题目 点此看题 好像某谷还没有翻译,建议翻译看看第一篇题解(还有一个提示比较重要) 二.解法 首先不考虑线段的限制,容易写出式子dp[i][j]=dp[i−1][j]+dp[i−1][j+1]+d ...

  7. CF821E 【Okabe and El Psy Kongroo】

    首先我们从最简单的dp开始 \(dp[i][j]=dp[i-1][j]+dp[i-1][j+1]+dp[i-1][j-1]\) 然后这是一个O(NM)的做法,肯定行不通,然后我们考虑使用矩阵加速 \( ...

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

    链接:http://codeforces.com/problemset/problem/821/E 分析:由于有边界而且不同段边界还不同,直接算是不行的..k是1e18,dp也不行..用一个16维的向 ...

  9. EL PSY CONGROO

    加粗样式 #include <bits/stdc++.h> using namespace std; int mod = 98319; vector<int> edge[100 ...

最新文章

  1. ycsb两个阶段说明
  2. Spring Boot Spring MVC异常处理原理分析
  3. python心得体会200字_50行代码让python自动生成文章
  4. python全栈工程师薪资-Python全栈工程师为何这么火薪资这么高看了才知道
  5. python写机器人程序_用Python写的一个多线程机器人聊天程序
  6. [渝粤教育] 长沙民政职业技术学院 高职公共英语(一) 参考 资料
  7. Ubuntu提示软件更新
  8. java私塾 代码_【整理】java私塾教程课后习题
  9. Fedora 9 vmware 上网问题
  10. Raspberry Pi 树莓派查看CPU温度
  11. 病人陈天桥,归来依旧是传奇(转)
  12. Java核心技术卷一 -第九章:集合
  13. [线性dp]leetcode2327:知道秘密的人数(medium)
  14. MarkDown-Typora MarkDown Reference
  15. 19张动作速写参考,人物灵活就靠这些!
  16. opencv图像归一化zscore_normalize
  17. 年历 | 19年前,腾讯QQ诞生
  18. A054_Linux本地部署_华为云远程部署
  19. tasklist 结束进程_转Tasklist(windows)
  20. 10个常用的Linux命令解析

热门文章

  1. 为什么我还在用Windows?
  2. 没有为此解决方案配置选中要生成的项目
  3. java装了1.8,更换成11后,-version还是显示java8的解决方法
  4. 根据城市查找编号,根据编号查找城市
  5. 中通快递发布2021 年第三季度未经审计财务业绩;2021年第十届Medidata NEXT中国年会开幕 | 全球TMT...
  6. pytest--fixture的使用(前置、后置)
  7. 63家企业上榜!华为云优秀合作伙伴公布!
  8. 全球最神秘的高频交易巨头
  9. 调用百度接口实现人脸识别
  10. 登陆网站中的验证码是起什么作用的