Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo 矩阵快速幂优化dp
2 seconds
256 megabytes
standard input
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 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.
The first line of input contains the integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 1018) — the number of segments and the destination x coordinate.
The next n lines contain three space-separated integers ai, bi, 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.
Print the number of walks satisfying the conditions, modulo 1000000007 (109 + 7).
1 3
0 3 3
4
2 6
0 3 0
3 10 2
4
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:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<bitset>
#include<time.h>
using namespace std;
#define LL long long
#define pi (4*atan(1.0))
#define eps 1e-4
#define bug(x) cout<<"bug"<<x<<endl;
const int N=3e5+,M=4e6+,inf=,mod=1e9+;
const LL INF=1e18+,MOD=1e9+; struct Matrix
{
LL a[][];
Matrix()
{
memset(a,,sizeof(a));
}
void init()
{
for(int i=;i<;i++)
for(int j=;j<;j++)
a[i][j]=(i==j);
}
Matrix operator + (const Matrix &B)const
{
Matrix C;
for(int i=;i<;i++)
for(int j=;j<;j++)
C.a[i][j]=(a[i][j]+B.a[i][j])%MOD;
return C;
}
Matrix operator * (const Matrix &B)const
{
Matrix C;
for(int i=;i<;i++)
for(int k=;k<;k++)
for(int j=;j<;j++)
C.a[i][j]=(C.a[i][j]+1LL*a[i][k]*B.a[k][j])%MOD;
return C;
}
Matrix operator ^ (const LL &t)const
{
Matrix A=(*this),res;
res.init();
LL p=t;
while(p)
{
if(p&)res=res*A;
A=A*A;
p>>=;
}
return res;
}
};
map<pair<LL,int> ,LL >dp;
LL a[N],b[N];int c[N];
Matrix Gbase(int n)
{
Matrix a;
a.init();
for(int i=;i<=n;i++)
{
if(i->=)a.a[i-][i]=;
a.a[i][i]=;
if(i+<=n)a.a[i+][i]=;
}
return a;
}
Matrix Gpre(LL x,int n)
{
Matrix a;
a.init();
for(int i=;i<=n;i++)
a.a[][i]=dp[make_pair(x,i)];
return a;
}
int main()
{
int n;
LL k;
scanf("%d%lld",&n,&k);
for(int i=;i<=n;i++)
scanf("%lld%lld%d",&a[i],&b[i],&c[i]);
dp[make_pair(,)]=;
for(int i=;i<=n;i++)
{
Matrix base=Gbase(c[i]);
Matrix pre=Gpre(a[i],c[i]);
LL l=a[i],r=min(b[i],k);
base=base^(r-l);
Matrix ans=pre*base;
for(int j=;j<=c[i];j++)
dp[make_pair(r,j)]=ans.a[][j];
if(b[i]>=k)break;
}
printf("%lld\n",dp[make_pair(k,)]);
return ;
}
Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo 矩阵快速幂优化dp的更多相关文章
- 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 ...
- 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 ...
- CF821 E. Okabe and El Psy Kongroo 矩阵快速幂
LINK 题意:给出$n$条平行于x轴的线段,终点$k$坐标$(k <= 10^{18})$,现在可以在线段之间进行移动,但不能超出两条线段的y坐标所夹范围,问到达终点有几种方案. 思路:刚开始 ...
- Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations (矩阵高速幂)
题目地址:http://codeforces.com/contest/551/problem/D 分析下公式能够知道,相当于每一位上放0或者1使得最后成为0或者1.假设最后是0的话,那么全部相邻位一定 ...
- Codeforces Round #189 (Div. 1) C - Kalila and Dimna in the Logging Industry 斜率优化dp
C - Kalila and Dimna in the Logging Industry 很容易能得到状态转移方程 dp[ i ] = min( dp[ j ] + b[ j ] * a[ i ] ) ...
- Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations 矩阵快速幂优化dp
D. GukiZ and Binary Operations time limit per test 1 second memory limit per test 256 megabytes inpu ...
- Codeforces Round #420 (Div. 2)
/*************************************************************************************************** ...
- Codeforces Round #420 (Div. 2) A-E
本来打算划划水洗洗睡了,突然听到这次的主人公是冈部伦太郎 石头门(<steins;gate>)主题的比赛,岂有不打之理! 石头门真的很棒啊!人设也好剧情也赞曲子也特别好听. 推荐http: ...
- 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 ...
随机推荐
- 基于jquery、bootstrap的数据验证插件bootstrapValidator使用
实时验证用户名是否存在,密码不能和用户名相同,两次密码需要相同,提交之后需要验证返回值: <form id="defaultForm" role="form&quo ...
- ASP.NET Core Web API 索引 (更新Redis in .NET Core)
https://www.cnblogs.com/cgzl/p/9178672.html
- [转载]oracle树形查询 start with connect by
一.简介 在oracle中start with connect by (prior) 用来对树形结构的数据进行查询.其中start with conditon 给出的是数据搜索范围, connect ...
- Selenium+Java自动化测试的方法
1.设置等待时间Thread.sleep(2000); (1000代表1s)2.断言assertion:验证应用程序的状态是否同所期望的一致.常见的断言包括:验证页面内容,如标题是否为X或当前位置是否 ...
- Django框架----logging配置
我写Django项目常用的logging配置.(追加在setting.py文件中) LOGGING = { 'version': 1, 'disable_existing_loggers': Fals ...
- 怎样从外网访问内网Apache HTTP Server
本地安装了一个Apache HTTP Server,只能在局域网内访问,怎样从外网也能访问到本地的Apache HTTP Server呢?本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动 ...
- mycat分片操作
mycat位于应用与数据库的中间层,可以灵活解耦应用与数据库,后端数据库可以位于不同的主机上.在mycat中将表分为两大类:对于数据量小且不需要做数据切片的表,称之为分片表:对于数据量大到单库性能,容 ...
- Golang切片的三种简单使用方式及区别
概念 切片(slice)是建立在数组之上的更方便,更灵活,更强大的数据结构.切片并不存储任何元素而只是对现有数组的引用. 三种方式及细节案例 ①定义一个切片,然后让切片去引用一个已经创建好的数组 pa ...
- 初识wxPython
wxPython是包装C++编写的wxWidgets跨平台的GUI组件 安装wxPython pip install wxpython import wx def load(event): file ...
- Centos7.3+uwsgi+Nginx部署Django程序
1. 安装Python,这里我用的是阿里云的centos7.3,自带python2,所以,此步略过,具体安装Python可Google. 2. 安装uwsgi,如果安装失败的话首先,我的Python解 ...