zjuoj 3604 Tunnel Network
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3604
Tunnel Network
Time Limit: 2 Seconds Memory Limit: 65536 KB
Country Far-Far-Away is a big country with N cities. But it is now under a civil war. The rebel uses the ancient tunnel network which connects all N cities with N-1 inter-city tunnels for transportation. The government army want to destroy these tunnels one by one. After several months fighting, some tunnels have been destoryed. According to the intel, the tunnel network have excatly S connected components now. And what government army further knows is that city 1, 2, ... , S belong to each of the S connected components. Since the government have little knowledge about the remaining tunnels, they ask you to calculate the number of possible networks of remaining tunnels.
Input
There are multiple test cases. The first line of input contains an integer T (T ≤ 500) indicating the number of test cases. Then T test cases follow.
Each case contains one line containing two integer N (2 ≤ N ≤ 2000) and S (2 ≤ S ≤ N), as described above.
Output
The number of possible networks now. Since the number might be very large, you should output the answer after modulo 1000000007.
Sample Input
4
3 2
4 2
5 3
100 50
Sample Output
2
8
15
113366355
Author: WANG, Yelei
Contest: The 9th Zhejiang Provincial Collegiate Programming Contest
分析:
不过对于给定的n和s,可以对n-s个点先排列然后划分成s份,然后计算有x个点树有多少种。这样显然不好递推,也没法写公式。事实上对任意n个点构成的树对应一个长为n-2的Prufer序列,且这种关系是一一对应。所谓Prufer序列是每次拿掉编号最小的叶节点然后将它的父亲push,直到只剩下两个节点。因此可以假定有点0与s个城市分别相连,问题转化为这n+1个点构成的树的Prufer序列有多少种。注意到序列倒数第s个数必为1~s中的某一个且最后s-1个数必为根节点0,于是排列数为n^(n-s-1)*s。
prufer序列:http://www.cnblogs.com/jeff-wgc/p/4472528.html
AC代码:
#include <cstdio>
#define Mod 1000000007 int T, s, t;
long long n, r; inline int cal()
{
if(n == s) return ;
t = n-s-, r = s;
while(t > )
{
if(t&) r=(r*n)%Mod;
t >>=, n=(n*n)%Mod;
}
return r;
} int main()
{
scanf("%d", &T);
while(T --)
{
scanf("%d%d", &n, &s);
printf("%d\n", cal());
}
return ;
}
zjuoj 3604 Tunnel Network的更多相关文章
- ZOJ 3604 Tunnel Network(凯莱定理)
题目链接: E - Tunnel Network ZOJ - 3604 题目大意: 给定编号1-n的点,和给定编号1-S 的联通图,刚开始1号联通图只有 1个顶点,就是编号为1的顶点,2号联通图也只有 ...
- wpa_supplicant.conf
转自:http://w1.fi/gitweb/gitweb.cgi?p=hostap.git;a=blob_plain;f=wpa_supplicant/wpa_supplicant.conf ### ...
- Networking in too much detail
The players This document describes the architecture that results from a particular OpenStack config ...
- wpa_supplicant软件架构分析
wpa_supplicant软件架构分析 1. 启动命令 wpa supplicant 在启动时,启动命令可以带有很多参数,目前我们的启动命令如下: wpa_supplicant /system/bi ...
- D01 Elon Mulsk The future we're building — and boring
摘要:精选TED. 每个音频不超过2分钟,学英语和吸收伟大思想两不误 音频: https://n1audio.hjfile.cn/st/fb5ace6f-7b63-439d-954c-c4539c1f ...
- D02 TED Elon Mulsk The future we're building — and boring
摘要:精选TED. 每个音频不超过2分钟,学英语和吸收伟大思想两不误 音频: https://n1audio.hjfile.cn/st/de745862-d5f4-4ef4-9218-d79361ca ...
- 三:OVS+GRE之完整网络流程
知识点一:linux网桥提供安全组 知识点二:每新建一个网络,在网络节点都会新建一个namespace,只要为该网络建立子网,那么该namespace里就新增dhcp来为该子网分配ip,也可以为该网络 ...
- 2012-2014 三年浙江 acm 省赛 题目 分类
The 9th Zhejiang Provincial Collegiate Programming Contest A Taxi Fare 25.57% (166/649) (水 ...
- openstack-ansible Chapter 4. Deployment configuration
Initial environment configuration Copy the contents of the /opt/openstack-ansible/etc/openstack_depl ...
随机推荐
- C语言中if语句
C语言if语句后面的表达式 C语言中if关键字之后(即括号内)均为表达式. 该表达式通常是逻辑表达式或关系表达式,但也可以是其它表达式,如赋值表达式等,甚至也可以是一个变量,这些变量的值都换算成了逻辑 ...
- 解决方案:Resharper对系统关键字提示‘can not resolve symbol XXX’,并且显示红色,但是编译没有问题
环境:Visual studio 2013 community update 4 + Resharper 8.2 + Windows 7现象:我的C#工程编译没有问题, 但是在代码编辑器中系统关键字显 ...
- 七、考反映小游戏《苹果iOS实例编程入门教程》
该app为应用的功能为一个简单的考反应游戏 纲要:-UIButton, UILabel, UIImageView 的运用:-利用rendom增加游戏可玩性: 游戏说明: 在按下开始游戏后,分为三盏的指 ...
- 【转】C#进阶系列——WebApi 接口参数不再困惑:传参详解
原文地址:http://www.cnblogs.com/landeanfen/archive/2016/04/06/5337072.html 阅读目录 一.get请求 1.基础类型参数 2.实体作为参 ...
- checkbox全选与反选
用原生js跟jquery实现checkbox全选反选的一个例子 原生js: <!DOCTYPE html> <html lang="en"> <hea ...
- 在MacOS和iOS系统中使用OpenCV
在MacOS和iOS系统中使用OpenCV 前言 OpenCV 是一个开源的跨平台计算机视觉库,实现了图像处理和计算机视觉方面的很多通用算法. 最近试着在 MacOS 和 iOS 上使用 OpenCV ...
- [CareerCup] 16.6 Synchronized Method 同步方法
16.6 You are given a class with synchronized method A and a normal method B. If you have two threads ...
- Window.document对象 轮播练习
Window.document对象 一.找到元素: docunment.getElementById("id"):根据id找,最多找一个: var a =docun ...
- uva705--slash maze
/*这道题我原本是将斜线迷宫扩大为原来的两倍,但是在这种情况下对于在斜的方向上的搜索会变的较容易出错,所以参考了别人的思路后将迷宫扩展为原来的3倍,这样就变成一般的迷宫问题了*/ #include&q ...
- github上怎么预览页面
直接在 http://htmlpreview.github.io/? 后面加上git上的地址就可以预览了 比如 http://htmlpreview.github.io/?https://github ...