LINK

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

思路:刚开始以为限制只是到达线段上就必须沿线段走,后来才发现是要求走y坐标所夹范围,那么就简单多了,很容易看出是个递推形DP,然而数据量有点大,k为10的18次,一般转移显然不可行。由于是个递推,而且y坐标最大也只有15,故使用矩阵优化递推复杂度即可。

/** @Date    : 2017-07-04 16:06:18
* @FileName: E 矩阵快速幂 + 递推.cpp
* @Platform: Windows
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version : $Id$
*/
#include <bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8;
const LL mod = 1e9 + 7;
int len;
LL n, k;
struct yuu
{
LL mat[18][18];
yuu(){MMF(mat);}
void init()
{
for(int i = 0; i <= 17; i++)
mat[i][i] = 1;
}
yuu operator *(const yuu &b)
{
yuu c;
for(int i = 0; i <= len; i++)
{
for(int j = 0; j <= len; j++)
{
for(int k = 0; k <= len; k++)
{
c.mat[i][j] = (c.mat[i][j] + this->mat[i][k] * b.mat[k][j] % mod) % mod;
}
}
}
return c;
}
};
yuu fpow(yuu a, LL n)
{
yuu res;
res.init();
while(n)
{
if(n & 1)
res = res * a;
a = a * a;
n >>= 1;
}
return res;
} int main()
{
while(cin >> n >> k)
{
yuu A, B, t;
for(int i = 0; i < 16; i++)
{
int x = i - 1;
if(x < 0)
A.mat[i][x + 1] = 1;
else A.mat[i][x] = 1;
A.mat[i][x + 1] = A.mat[i][x + 2] = 1;
} t.mat[0][0] = 1;
int flag = 0;
for(int i = 0; i < n; i++)
{
LL l, r, c;
scanf("%lld%lld%lld", &l, &r, &c);
if(flag)
continue;
flag = 0;
r = min(r, k);
if(r == k)
flag = 1;
len = c;
B = fpow(A, r - l);
for(int i = c + 1; i < 16; i++)
t.mat[i][0] = 0;
B = B * t;
for(int i = 0; i <= len; i++)
t.mat[i][0] = B.mat[i][0]; }
printf("%lld\n", B.mat[0][0]);
} return 0;
}

CF821 E. Okabe and El Psy Kongroo 矩阵快速幂的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. codeforces E. Okabe and El Psy Kongroo(dp+矩阵快速幂)

    题目链接:http://codeforces.com/contest/821/problem/E 题意:我们现在位于(0,0)处,目标是走到(K,0)处.每一次我们都可以从(x,y)走到(x+1,y- ...

  6. 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)的做法,肯定行不通,然后我们考虑使用矩阵加速 \( ...

  7. [codeforces821E]Okabe and El Psy Kongroo

    题意:(0,0)走到(k,0),每一部分有一条线段作为上界,求方案数. 解题关键:dp+矩阵快速幂,盗个图,注意ll 关于那条语句为什么不加也可以,因为我的矩阵C,就是因为多传了了len的原因,其他位 ...

  8. Codeforces 821E Okabe and El Psy Kongroo

    题意:我们现在位于(0,0)处,目标是走到(K,0)处.每一次我们都可以从(x,y)走到(x+1,y-1)或者(x+1,y)或者(x+1,y+1)三个位子之一.现在一共有N段线段,每条线段都是平行于X ...

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

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

随机推荐

  1. 20162328蔡文琛 Java课程总结

    20162328 2016-2017-2<程序设计与数据结构>课程总结 一.每周作业.结对编程博客的链接汇总 预备作业01 20162328:表达对专业的期许.浅谈师生关系.对未来学习任务 ...

  2. css全局样式基础代码

    body{ font-size:12px; font-family:"宋体",Arial, Helvetica, sans-serif;color:#363636;backgrou ...

  3. [2017BUAA软工]第二次博客作业:代码复审

    〇.comment链接 https://github.com/hanayashiki/Sudoku/issues/1 一.代码复审 1.概要部分 (1)代码能符合需求和规格说明么? 经测试,对于合法输 ...

  4. 2nd 简单四则运算更新

    简单四则运算更新 功能:由随机数决定出题为10个以内的数字,并确定是否出现括号(仅限一对),顺序输出表达式,并用栈的方式进行计算,判断正误.其他功能有待进一步实现. 头文件 #include < ...

  5. mybatis update数据时无异常但没更新成功;update异常时如数据超出大小限制,造成死锁

    没更新的问题原因: sqlSession.commit(); 没执行commit,但官方文档里有这样的描述:“默认情况下 MyBatis 不会自动提交事务,除非它侦测到有插入.更新或删除操作改变了数据 ...

  6. websocket服务器+客户端

    <?php $demo = new ws('192.168.90.47',12345); $demo->run(); class ws { //当前服务端主连接 private $curr ...

  7. fsocket发送post实现异步请求

    function triggerRequest($url, $post_data = array(), $cookie = array()){ //可以通过POST或者GET传递一些参数给要触发的脚本 ...

  8. laraven安装记录

    版本4.2.11 下载地址:https://codeload.github.com/laravel/laravel/zip/v4.2.11 步骤: 1.解压到目录 2.下载composer,并放到/u ...

  9. 更新user的方法

    from django.contrib.auth.admin import UserAdmin from django.contrib.auth.forms import UserChangeForm ...

  10. 两个float 怎么比较大小

    转自:http://blog.csdn.net/mydriverc2/article/details/49888947 float 类型不能比较相等或不等,但可以比较>,<,>=,& ...