2019 ICPC 沈阳网络赛 J. Ghh Matin
Problem
Similar to the strange ability of Martin (the hero of Martin Martin), Ghh will random occurrence in one of \(N\) cities every morning and the money will change to \(X\) RMB (No matter how much money had yesterday).
Ghh finds that every \(N\) cities have only one way to enter and go out (for example, city A's out road is connected with city B's enter road, or city A's out road may also be connected with city A's enter road). After waking up one day, Ghh suddenly wants to do an experiment.
In the following days, Ghh will judge whether he has experimented in the city every time he wakes up. If not, he will go out to see if he can start from the city and finally return to itself. If he can't or has already done it, he will stay until the evening and go to the next random city by sleeping. This experiment lasts until every \(N\) cities have been done.
Because the distances between cities are very far, Ghh decides to take bus. The cost of bus between any two cities is \(1\) RMB (it also costs \(1\) RMB for the city that the enter road is connected with the out road of itself) and the speed is very fast. What is the probability that Ghh can return to the city itself from every city when Ghh has finished the experiment?
Note
When \(n=2\), there are two cases in city A and B:
- A connected with A, B connected with B
- A connected with B
When \(X\) is 1, only in the first case every city can return to itself, so the probability is 1/2. When \(X\) is 2, any case is ok.
Input
The first line, input a integer \(T\) represents the number of test cases (\(T \le 10^4\)). Next \(2\) ~ \(T+1\) line, input two integers \(N\), \(X\) represents the total number of cities and initial money (\(2≤N≤10^6\),\(N≤2X≤2×10^9\)). \(\sum{N} \le 10^7\).
Output
Each test case output one line, the probabilities may be \(a/b\), please output \(a*inv(b)\mod (10^9+7)\). \(inv(b)\) is the inverse of \(b\).
样例输入复制
3
2 1
2 2
145 134
样例输出复制
500000004
1
772215686
Comprehension
题意大概是说:有n个城市,每个城市只有以一种进出方式(入度、出度相同),从一个城市去另一个城市要1块钱。现在不知道这个图是什么样的,现在需要能从每个城市出发(有X块钱)能回到该城市,得到这样的图的概率是多少?
也就是说:目标的图肯定是很多欧拉回路组成的图,问每条欧拉回路的长度都不大于X的概率是多少?
Solve
根据对题意的理解,我们发现不好枚举这样的图,不如去枚举所有不满足该条件的图,也就是说去找存在欧拉回路长度大于X的图。
题目限制了\(N\le 2X\) ,这就是说有X点的欧拉回路,就没有X+1的
很显然,选出大于X个点连成环,剩下点自由组合。
我们不妨设大于X个点是m,\(X<m<N\)
\(P(m)=\)存在欧拉回路长度大于X的图的数量/总图数
存在欧拉回路长度大于X的图的数量=N个里面选m个 \(\times\) m-1(环内)的全排列(因为起点是固定的 \(\times\) n-m(剩下的点)的全排列
\(P(m)=C(m,N)*(m-1)!*(N-m)!/N!=1/m\)
所以\(ans=1-\sum\limits_{m>X}^N{P(m)}\)
因为\(P(m)\) 最大到N,是1e6,所以开个前缀和维护一下,省时间
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int maxn = 1e6+4;
ll qpow(ll a,ll b){
ll ans=1;
while(b){
if(b&1)
ans=(ans*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return ans%mod;
}
ll inv(ll a){
return qpow(a,mod-2);
}
ll sum[maxn];
int main()
{
int t;
ll n,x;
for(int i = 1; i<maxn;i++){
sum[i]=(sum[i-1]+inv(i))%mod;
}
scanf("%d",&t);
while(t--) {
scanf("%lld%lld", &n, &x);
ll ans=1;
if(n>x)
ans = (ans+ mod -(sum[n]-sum[x]))%mod;
cout<<ans<<endl;
}
return 0;
}
2019 ICPC 沈阳网络赛 J. Ghh Matin的更多相关文章
- 2018 ICPC 沈阳网络赛
2018 ICPC 沈阳网络赛 Call of Accepted 题目描述:求一个算式的最大值与最小值. solution 按普通算式计算方法做,只不过要同时记住最大值和最小值而已. Convex H ...
- 2019 ICPC 南昌网络赛
2019 ICPC 南昌网络赛 比赛时间:2019.9.8 比赛链接:The 2019 Asia Nanchang First Round Online Programming Contest 总结 ...
- 计蒜客 2019南昌邀请网络赛J Distance on the tree(主席树)题解
题意:给出一棵树,给出每条边的权值,现在给出m个询问,要你每次输出u~v的最短路径中,边权 <= k 的边有几条 思路:当时网络赛的时候没学过主席树,现在补上.先树上建主席树,然后把边权交给子节 ...
- 2019年ICPC南昌网络赛 J. Distance on the tree 树链剖分+主席树
边权转点权,每次遍历到下一个点,把走个这条边的权值加入主席树中即可. #include<iostream> #include<algorithm> #include<st ...
- 2017 icpc 沈阳网络赛
cable cable cable Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 2019南昌邀请赛网络赛:J distance on the tree
1000ms 262144K DSM(Data Structure Master) once learned about tree when he was preparing for NOIP(N ...
- 2019 ICPC南京网络赛 F题 Greedy Sequence(贪心+递推)
计蒜客题目链接:https://nanti.jisuanke.com/t/41303 题目:给你一个序列a,你可以从其中选取元素,构建n个串,每个串的长度为n,构造的si串要满足以下条件, 1. si ...
- 2019 ICPC 银川网络赛 F-Moving On (卡Cache)
Firdaws and Fatinah are living in a country with nn cities, numbered from 11 to nn. Each city has a ...
- 2019 ICPC 南京网络赛 F Greedy Sequence
You're given a permutation aa of length nn (1 \le n \le 10^51≤n≤105). For each i \in [1,n]i∈[1,n], c ...
随机推荐
- Python之特征工程-3
一.什么是特征工程?其实也是数据处理的一种方式,和前面的原始数据不一样的是,我们在原始数据的基础上面,通过提取有效特征,来预测目标值.而想要更好的去得出结果,包括前面使用的数据处理中数据特征提取,新增 ...
- AdventureWorks 安装和配置[转自 微软msdn]
AdventureWorks 安装和配置 2018/06/19 适用对象:SQL ServerAzure SQL 数据库Azure SQL 数据仓库并行数据仓库 AdventureWorks 下载链接 ...
- JavaScript 基础(数据类型、函数、流程控制、对象)
一.JavaScript概述 1.1 JavaScript的历史 1992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在CEnvi软件中).后将其改名Script ...
- H5 - 简学
什么是HTML? HTML 是用来描述网页的一种语言. 0.HTML 指的是超文本标记语言 1.HTML 不是一种编程语言,而是一种标记语言 2.标记语言是一套标记标签 3.HTML 使用标记标签来描 ...
- QuickJS 快速入门 (QuickJS QuickStart)
1. QuickJS 快速入门 (QuickJS QuickStart) 1. QuickJS 快速入门 (QuickJS QuickStart) 1.1. 简介 1.2. 安装 1.3. 简单使用 ...
- putty使用方法
putty是一种体体积小,无需安装的一款免费安全使用方便的绿色软件,它主要用于远程控制linux系统,只要获取了远程的linux的地址,便可以远程控制linux系统以方便管理,越来越受到各方面的欢迎. ...
- Java 之 File类(文件操作)
一.概述 java.io.File 类是文件和目录路径名册抽象表示,主要用于文件和目录的创建.查找和删除等操作. File类是一个与系统无关的类,任何的操作系统都可以使用这个类中的方法. 路径问题: ...
- hashmap,hashtable,concurrenthashmap多线程下的比较(持续更新)
1.hashMap 多线程下put会造成死循环,主要是扩容时transfer方法会造成死循环. http://blog.csdn.net/zhuqiuhui/article/details/51849 ...
- 打造完美Python环境(pyenv, virtualenv, pip)
写在最前 在使用 Python 进行开发和部署的时候,经常会碰到Python版本或者依赖包或者对应版本不同导致各种意外情况发生. 本文将介绍如何通过 pyenv, virtualenv, pip三个工 ...
- windows和linux下的spice客户端使用方法
1.Linux客户端 安装spice yum install virt-viewer 连接远程虚拟机 #remote-viewer spice://IP:PORTremote-viewer spice ...