[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. 【leetcode 简单】第十二题 报数

    报数序列是指一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111221 1 被读作  "one 1&quo ...

  2. php常用函数——数组函数

    php常用函数——数组函数

  3. 31 - gogs安装-git基础

    目录 1 Gogs安装 2 Git介绍 3 使用Github仓库 3.1 Git配置 3.2 远程仓库 4 Git基本使用 4.1 创建版本库 4.2 查看工作区状态 4.3 查看修改内容 4.4 查 ...

  4. [转载]C++多态技术

    摘自: http://www.royaloo.com/articles/articles_2003/PolymorphismInCpp.htm http://blog.sciencenet.cn/bl ...

  5. spring集成swagger

    随着互联网技术的发展,现在的网站架构基本都由原来的后端渲染,变成了:前端渲染.前后端分离的形态,而且前端技术和后端技术在各自的道路上越走越远. 前端和后端的唯一联系,变成了API接口:API文档变成了 ...

  6. UFLDL 教程学习笔记(六)主成分分析

    教程:http://ufldl.stanford.edu/tutorial/supervised/MultiLayerNeuralNetworks/ 以及这篇博文,写的很清楚:http://blog. ...

  7. s3cmd : Add a config parameter to enable path-style bucket access 当ceph rgw使用域名时,需要支持 path-style bucket特性

    s3cmd 要是1.6.1 之后的版本 增加配置项:  vi .s3cfg use_path_mode = True 源码参考: cat  /usr/local/lib/python2.7/dist- ...

  8. MySQL之正则表达式

    一.介绍 正则表达式用来描述或者匹配符合规则的字符串.它的用法和like比较相似,但是它又比like更强大,能够实现一些很特殊的规则匹配:正则表达式需要使用REGEXP命令,匹配上返回"1& ...

  9. linux 下vim文件乱码 cat文件正常处理方法

    linux 下vim文件乱码 cat文件正常处理方法 服务器支持中文字符集,cat和其他查看文件命令现在正常,vim还是出现了中文乱码问题, 1.查看文件编码格式 vim 文件 :set fileen ...

  10. find tar排除指定文件或目录操作及查找文件内容关键字

    1.find查找排除单个目录 查找当前目录或者子目录下所有.txt文件,但是跳过子目录sk find . -path "./sk" -prune -o -name "*. ...