Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo DP+矩阵快速幂加速
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.
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 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
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+矩阵快速幂加速的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #420 (Div. 2)
/*************************************************************************************************** ...
- Codeforces Round #420 (Div. 2) A-E
本来打算划划水洗洗睡了,突然听到这次的主人公是冈部伦太郎 石头门(<steins;gate>)主题的比赛,岂有不打之理! 石头门真的很棒啊!人设也好剧情也赞曲子也特别好听. 推荐http: ...
- codeforces E. Okabe and El Psy Kongroo(dp+矩阵快速幂)
题目链接:http://codeforces.com/contest/821/problem/E 题意:我们现在位于(0,0)处,目标是走到(K,0)处.每一次我们都可以从(x,y)走到(x+1,y- ...
- Codeforces Round #420 (Div. 2) - E
题目链接:http://codeforces.com/contest/821/problem/E 题意:起初在(0,0),现在要求走到(k,0),问你存在多少种走法. 其中有n条线段,每条线段为(a, ...
- 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 ...
- Educational Codeforces Round 60 D dp + 矩阵快速幂
https://codeforces.com/contest/1117/problem/D 题意 有n个特殊宝石(n<=1e18),每个特殊宝石可以分解成m个普通宝石(m<=100),问组 ...
随机推荐
- Codeforces Round #401 (Div. 2) 离翻身就差2分钟
Codeforces Round #401 (Div. 2) 很happy,现场榜很happy,完全将昨晚的不悦忘了.终判我校一片惨白,小董同学怒怼D\E,离AK就差一个C了,于是我AC了C题还剩35 ...
- shell的brake和continue的用法
在循环过程中,有时候需要在未达到循环结束条件时强制跳出循环,像大多数编程语言一样,Shell也使用 break 和 continue 来跳出循环. break命令 break命令允许跳出所有循环(终止 ...
- Ignite集成Spark之IgniteDataFrames
下面简要地回顾一下在第一篇文章中所谈到的内容. Ignite是一个分布式的内存数据库.缓存和处理平台,为事务型.分析型和流式负载而设计,在保证扩展性的前提下提供了内存级的性能. Spark是一个流式数 ...
- 2013 Asia acm Hangzhou Regional Contest 杭州现场赛
B Stealing Harry Potter's Precious 题目大意:给定一个n*m的地图,某些点可以走,某些点可以走某些点不可以走,给定一个起点,又给出了k个点k<=4,要求从起点 ...
- 转载:hmm学习网站
http://www.52nlp.cn/hmm-learn-best-practices-seven-forward-backward-algorithm-5
- Nova 组件详解
本节开始,我们将详细讲解 Nova 的各个子服务. 前面架构概览一节知道 Nova 有若干 nova-* 的子服务,下面我们将依次学习最重要的几个.今天先讨论 nova-api 和 nova-cond ...
- plsql + 客户端 连接oracle数据库
一. 目录结构D:\oracle\instantclient_11_2D:\oracle\instantclient_11_2\tnsnames.ora 二. 环境变量 NLS_LANG = SIMP ...
- Mac OS X 下安装python的MySQLdb模块
参考资料: mac os x下python安装MySQLdb模块 http://www.codeif.com/post/1073/ MAC OSX使用Python安装模块有关问题 http:// ...
- route命令走一波
1.写文章去了解某个命令完全是兴起,并没有下定决心去学哪一套课程,目前的状态仍然是犹豫中,废话不多说进入正文,九月二十七这天看到了route命令,发现对路由这个命令很陌生,陌生到根本不知道它是用来干什 ...
- T9270 mjt树
题目背景 从前森林里有一棵很大的mjt树,树上有很多小动物. 题目描述 mjt树上有 n 个房间,第 i 个房间住着 ai 只第bi 种小动物. 这n个房间用n-1条路连接起来,其中房间1位mjt树的 ...