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),问组 ...
随机推荐
- 九度oj 题目1369:字符串的排列
题目描述: 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. 输入: 每个 ...
- 算法复习——cdq分治
题目: Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A比另一朵花B要 ...
- Caffe的Solver参数设置
Caffe的solver参数设置 http://caffe.berkeleyvision.org/tutorial/solver.html solver是通过协调前向-反向传播的参数更新来控制参数优化 ...
- Tomcat 调优技巧
Tomcat 调优技巧:1.Tomcat自身调优: ①采用动静分离节约Tomcat的性能: ②调整Tomcat的线程池: ③调整Tomcat的连接器: ④修改Tomcat的运行模式: ⑤禁用AJP连接 ...
- spring-Ioc容器与bean
1. spring 的beanFactory容器 bean.xml HelloWorld实体类与spring教程学习笔记1相同 public static void main(String[] ar ...
- jQuery插件封装系列(一)—— 金额录入框
基于jQuery原型封装数值录入框,禁止录入.粘贴非数值字符 (function ($) { // 数值输入框 $.fn.numbox = function (options) { var type ...
- Spring 定时器 定时访问数据库并发送邮件
我这里有两个案例的方法: 第一种:使用Spring quartz: 我这里使用的jar:spring-context-support.jar.quartz-1.6.5.jar ============ ...
- 如何选择 IT 技术书籍
★第1招:看网上评论 首先,上一些权威的图书网站,看看大伙儿的评价如何(要相信群众的眼睛是雪亮的).对于英文书籍,我一般上亚马逊网站去看看:中文书籍则上豆瓣网.这两个网站都提供星级评分,一般 > ...
- 分布式架构和微服务CI/CD的范本技术解读
随笔分类 - 分布式架构--http://www.cnblogs.com/hujihon/category/858846.html (ZooKeeper.activemq.redis.kafka)的分 ...
- Struts2 与SpringMVC之比较
1.Struts2是类级别的拦截, 一个类对应一个request上下文,SpringMVC是方法级别的拦截,一个方法对应一个request上下文,而方法同时又跟一个url对应,所以说从架构本身上Spr ...







