[CC-CHEFGRPH]Time to Study Graphs with Chef

题目大意:

一个有向图可以分成\(n+2(n\le10^{12})\)层,第\(0\)层和第\(n+1\)层有\(1\)个点,剩下每一层\(m(m\le10^5)\)个点。每个点到下一层的每一个点都有连边。另外有\(k(k\le5\times10^4)\)条新边,从层数小的点到层数大的点。

问从第\(0\)层到第\(n+1\)层有几种方案。

思路:

将额外边上的点离散出来单独计算答案,不是额外边的点可以直接通过快速幂计算贡献。

时间复杂度\(\mathcal O(k\log k)\)。

源代码:

#include<map>
#include<cstdio>
#include<cctype>
#include<vector>
#include<algorithm>
using int64=long long;
inline int64 getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int64 x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
using Point=std::pair<int64,int>;
constexpr int mod=1e9+7;
inline int power(int a,int64 k) {
int ret=1;
for(;k;k>>=1) {
if(k&1) ret=(int64)ret*a%mod;
a=(int64)a*a%mod;
}
return ret;
}
std::map<Point,std::vector<Point>> e;
std::map<Point,int> f;
std::map<int64,int> sum;
std::vector<int64> vx;
std::map<int64,std::vector<int>> vy;
int main() {
const int64 n=getint();
const int m=getint(),k=getint();
const Point s=(Point){0,0},t=(Point){n+1,0};
for(register int i=0;i<k;i++) {
const int64 sx=getint();
const int sy=getint();
const int64 tx=getint();
const int ty=getint();
const Point a=(Point){sx,sy},b=(Point){tx,ty};
e[b].emplace_back(a);
if(a!=s) {
vx.emplace_back(sx);
vy[sx].emplace_back(sy);
}
if(b!=t) {
vx.emplace_back(tx);
vy[tx].emplace_back(ty);
}
}
std::sort(vx.begin(),vx.end());
vx.resize(std::unique(vx.begin(),vx.end())-vx.begin());
sum[0]=f[s]=1;
for(register unsigned i=0;i<vx.size();i++) {
const int64 &x=vx[i],lastx=i?vx[i-1]:0;
auto &sumx=sum[x];
(sumx=(int64)sum[lastx]*power(m,x-lastx)%mod)%=mod;
auto &vyx=vy[x];
std::sort(vyx.begin(),vyx.end());
vyx.resize(std::unique(vyx.begin(),vyx.end())-vyx.begin());
for(register unsigned i=0;i<vyx.size();i++) {
const int &y=vyx[i];
const Point p=(Point){x,y};
const auto &ep=e[p];
auto &fp=f[p];
for(register unsigned i=0;i<ep.size();i++) {
const Point &q=ep[i];
(fp+=f[q])%=mod;
}
(sumx+=fp)%=mod;
(fp+=(int64)sum[lastx]*power(m,x-lastx-1)%mod)%=mod;
}
}
(f[t]=(int64)sum[vx.empty()?0:*vx.rbegin()]*power(m,n-(vx.empty()?0:*vx.rbegin()))%mod)%=mod;
for(register unsigned i=0;i<e[t].size();i++) {
(f[t]+=f[e[t][i]])%=mod;
}
printf("%d\n",f[t]);
return 0;
}

[CC-CHEFGRPH]Time to Study Graphs with Chef的更多相关文章

  1. 【hibernate】映射继承关系

    [hibernate]映射继承关系 转载:https://www.cnblogs.com/yangchongxing/p/10405151.html ========================= ...

  2. CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And Easy Xor Queries

    https://www.codechef.com/DEC17/problems/CHEFEXQ 题意: 位置i的数改为k 询问区间[1,i]内有多少个前缀的异或和为k 分块 sum[i][j] 表示第 ...

  3. CF&&CC百套计划2 CodeChef December Challenge 2017 Chef and Hamming Distance of arrays

    https://www.codechef.com/DEC17/problems/CHEFHAM #include<cstdio> #include<cstring> #incl ...

  4. CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And his Cake

    https://www.codechef.com/DEC17/problems/GIT01 #include<cstdio> #include<algorithm> using ...

  5. AIX Study之--AIX网卡配置管理(ent0、en0、et0)

    AIX Study之--AIX网卡配置管理(ent0.en0.et0) 1.查看AIX系统网卡信息: [root@aix211 /]#lsdev |grep et  en0 Available 1L- ...

  6. (转)Extracting knowledge from knowledge graphs using Facebook Pytorch BigGraph.

    Extracting knowledge from knowledge graphs using Facebook Pytorch BigGraph 2019-04-27 09:33:58 This ...

  7. [TF] Architecture - Computational Graphs

    阅读笔记: 仅希望对底层有一定必要的感性认识,包括一些基本核心概念. Here只关注Graph相关,因为对编程有益. TF – Kernels模块部分参见:https://mp.weixin.qq.c ...

  8. CF&&CC百套计划2 CodeChef December Challenge 2017 Total Diamonds

    https://www.codechef.com/DEC17/problems/VK18 #include<cstdio> #include<iostream> #includ ...

  9. Calculus on Computational Graphs: Backpropagation

    Calculus on Computational Graphs: Backpropagation Introduction Backpropagation is the key algorithm ...

随机推荐

  1. 详解H5中的history单页面,手动实现单页面开发,细说h5单页面原理

    就目前来看,前端的单页面开发占了很大一部分,一方面无刷新的切换增强了体验,并且浏览器记录依然存在,前进后退都没问题,在之前我们通地址栏中的hash改变来触发onhashchange方法来实现单页面应用 ...

  2. P2622 关灯问题II (状态压缩入门)

    题目链接: https://www.luogu.org/problemnew/show/P2622 具体思路:暴力,尝试每个开关,然后看所有的情况中存不存在灯全部关闭的情况,在储存所有灯的情况的时候, ...

  3. Linux内核多线程实现方法 —— kthread_create函数【转】

    转自:http://blog.csdn.net/sharecode/article/details/40076951 Linux内核多线程实现方法 —— kthread_create函数 内核经常需要 ...

  4. 认识Cookie和状态管理

    HTTP协议是一种无状态的协议,WEB服务器本身不能识别出哪些请求是同一个浏览器发出的 ,浏览器的每一次请求都是完全孤立的 即使 HTTP1.1 支持持续连接,但当用户有一段时间没有提交请求,连接也会 ...

  5. 浅谈Java中的hashcode方法(转)

    原文链接:http://www.cnblogs.com/dolphin0520/p/3681042.html 浅谈Java中的hashcode方法 哈希表这个数据结构想必大多数人都不陌生,而且在很多地 ...

  6. [ python ] 格式化输出、字符集、and/or/not 逻辑判断

    格式化输出 %: 占位符 s: 字符串 d: 数字 %%: 表示一个%, 第一个%是用来转义 实例: name = input('姓名:') age = int(input('年龄:')) print ...

  7. Vue.js——60分钟快速入门(转)

    var vm = new Vue({ el: '#app', data: { people: [{ name: 'Jack', age: 30, sex: 'Male' }, { name: 'Bil ...

  8. HDU 3533 Escape(BFS+预处理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3533 题目大意:给你一张n* m的地图,人在起点在(0,0)要到达终点(n,m)有k(k<=10 ...

  9. 机顶盒 gettimeofday()获取毫秒溢出

    最近在写代码的时候遇见了一个bug,在获取当前时间戳的毫秒时,我自己测试的时候总是OK的,但是测试那边总是测不对,之前一直以为是因为我存储的类型的不对,从long long类型从lld改成llu,然后 ...

  10. csu 1548(三分)

    1548: Design road Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: 383  Solved: 200[Submit][Status][We ...