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. OpenCV学习笔记——Mat类型数据存储

    CV_[The number of bits per item][Signed or Unsigned][Type Prefix]C[The channel number] 比如 CV_8UC3 表示 ...

  2. 细节--服务器mysql空密码

    在部署致服务器的时候 发现mysql密码为空的情况 如果采用 root账户的话 试过很多 比如不写下面这行 <property name="password" value=& ...

  3. 博弈---尼姆博奕(Nimm Game)(重点)

    尼姆博奕(Nimm Game):有三堆各若干个物品,两个人轮流从某一堆取任意多的 物品,规定每次至少取一个,多者不限,最后取光者得胜. 这种情况最有意思,它与二进制有密切关系,我们用(a,b,c)表示 ...

  4. Spring学习(五)——Spring注解(一)

    ---恢复内容开始--- 概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作.如使用 JPA 注释配置 ORM 映射 ...

  5. android入门 — ProgressDialog/DatePickerDialog/TimePickerDialog

    这三个Dialog都是AlertDialog的子类. ①DatePickerDialog 1.创建DatePickerDialog的实例: 2.通过Calendar类获得系统时间: 3.通过DateP ...

  6. HTML5 <meta> 标签属性,所有meta用法

    基本标签 声明文档使用的字符编码:<meta charset="utf-8" /> 声明文档的兼容模式:<meta http-equiv="X-UA-C ...

  7. DS01--抽象数据类型

    一.作业内容 二.数据结构.函数说明 1.头文件 common.h 2.数据结构 Rational.h 三.代码实现说明 1.构造有理数T 2.销毁有理数T 3.e返回有理数的分子或分母 4.用e改变 ...

  8. oracle和DB2的差异

    1.简介 当今IT的环境正经历着剧烈的变化,依靠单一的关系型数据库管理系统(RDBMS)管理数据的公司开始逐渐减少.分析家的报告指出 ,今天超过90%的公司都拥有不只一种RDBMS.在现在紧张的经济情 ...

  9. DNS服务器的解析

    ---恢复内容开始--- DNS前言: 英特网作为域名和IP地址相互映射的一个分不式数据库,能够使用户更方便的访问互联网.而不用去记住能够被机器直接读取的IP地址的过程叫做域名解析(或主机名解析).D ...

  10. QMultiMap使用

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QMultiMap使用     本文地址:http://techieliang.com/201 ...