[CC-CHEFGRPH]Time to Study Graphs with Chef
[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的更多相关文章
- 【hibernate】映射继承关系
[hibernate]映射继承关系 转载:https://www.cnblogs.com/yangchongxing/p/10405151.html ========================= ...
- 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] 表示第 ...
- 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 ...
- CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And his Cake
https://www.codechef.com/DEC17/problems/GIT01 #include<cstdio> #include<algorithm> using ...
- AIX Study之--AIX网卡配置管理(ent0、en0、et0)
AIX Study之--AIX网卡配置管理(ent0.en0.et0) 1.查看AIX系统网卡信息: [root@aix211 /]#lsdev |grep et en0 Available 1L- ...
- (转)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 ...
- [TF] Architecture - Computational Graphs
阅读笔记: 仅希望对底层有一定必要的感性认识,包括一些基本核心概念. Here只关注Graph相关,因为对编程有益. TF – Kernels模块部分参见:https://mp.weixin.qq.c ...
- CF&&CC百套计划2 CodeChef December Challenge 2017 Total Diamonds
https://www.codechef.com/DEC17/problems/VK18 #include<cstdio> #include<iostream> #includ ...
- Calculus on Computational Graphs: Backpropagation
Calculus on Computational Graphs: Backpropagation Introduction Backpropagation is the key algorithm ...
随机推荐
- java对象与json互转
package com.liveyc; import java.io.StringWriter; import org.junit.Test; import com.fasterxml.jackson ...
- 爬虫--Scrapy之Downloader Middleware
下载器中间件(Downloader Middleware) 下载器中间件是介于Scrapy的request/response处理的钩子框架. 是用于全局修改Scrapy request和respons ...
- windows下面安装Python和pip教程
第一步,先来安装Python.windows下面的Python安装一般是通过软件安装包安装而不是命令行,所以首先要在Python的官方主页上面下载最新的Python安装包.下载地址是:https:// ...
- Big O
评价一个计算机算法的效率时用到的方法我们称之为Big O(Order of [大约是]). 有序二分查找算法: 比如我们通过二分查找算法查找容器里的10个数据. 那么,我们需要查找一个数据时正常应该是 ...
- 夜神安卓模拟器adb命令详解
https://www.yeshen.com/faqs/H15tDZ6YW 一.如何找到adb? 安装夜神安卓模拟器后,电脑桌面会有"夜神模拟器"的启动图标,鼠标右键--打开文件所 ...
- Minimum Palindromic Factorization(最少回文串分割)
Minimum Palindromic Factorization(最少回文串分割) 以下内容大部分(可以说除了关于回文树的部分)来自论文A Subquadratic Algorithm for Mi ...
- fullpage.js 具体使用方法
1.fullpage.js 下载地址 https://github.com/alvarotrigo/fullPage.js 2.fullPage.js 是一个基于 jQuery 的插件,它能够很方便 ...
- StringBuilder基本用法
//StringBuilder用法 public class StringBuilderTest { public static void main(String[] args) { StringBu ...
- web项目更改文件后缀,隐藏编程语言
从Java EE5.0开始,<servlet-mapping>标签就可以配置多个<url-pattern>.例如可以同时将urlServlet配置一下多个映射方式: <s ...
- openssh升级步骤
1下载openssh最新版本 2 configure ./configure --prefix= /ssh先配置一下 再在本地安装. make &makeinstall 3 按照/ssh包含内 ...