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的更多相关文章

  1. ZOJ 3604 Tunnel Network(凯莱定理)

    题目链接: E - Tunnel Network ZOJ - 3604 题目大意: 给定编号1-n的点,和给定编号1-S 的联通图,刚开始1号联通图只有 1个顶点,就是编号为1的顶点,2号联通图也只有 ...

  2. wpa_supplicant.conf

    转自:http://w1.fi/gitweb/gitweb.cgi?p=hostap.git;a=blob_plain;f=wpa_supplicant/wpa_supplicant.conf ### ...

  3. Networking in too much detail

    The players This document describes the architecture that results from a particular OpenStack config ...

  4. wpa_supplicant软件架构分析

    wpa_supplicant软件架构分析 1. 启动命令 wpa supplicant 在启动时,启动命令可以带有很多参数,目前我们的启动命令如下: wpa_supplicant /system/bi ...

  5. D01 Elon Mulsk The future we're building — and boring

    摘要:精选TED. 每个音频不超过2分钟,学英语和吸收伟大思想两不误 音频: https://n1audio.hjfile.cn/st/fb5ace6f-7b63-439d-954c-c4539c1f ...

  6. D02 TED Elon Mulsk The future we're building — and boring

    摘要:精选TED. 每个音频不超过2分钟,学英语和吸收伟大思想两不误 音频: https://n1audio.hjfile.cn/st/de745862-d5f4-4ef4-9218-d79361ca ...

  7. 三:OVS+GRE之完整网络流程

    知识点一:linux网桥提供安全组 知识点二:每新建一个网络,在网络节点都会新建一个namespace,只要为该网络建立子网,那么该namespace里就新增dhcp来为该子网分配ip,也可以为该网络 ...

  8. 2012-2014 三年浙江 acm 省赛 题目 分类

    The 9th Zhejiang Provincial Collegiate Programming Contest A    Taxi Fare    25.57% (166/649)     (水 ...

  9. openstack-ansible Chapter 4. Deployment configuration

    Initial environment configuration Copy the contents of the /opt/openstack-ansible/etc/openstack_depl ...

随机推荐

  1. osg中的视点控制

    osg中的视点控制 osg的视点控制基类是CameraManipulator, 它是一个虚基类, 有用的方法都跟home有关. 在这个类里面有三个重要的成员变量: osg::Vec3d _homeEy ...

  2. Sping Environment为Null的原因和解决方法

    参考:https://github.com/spring-projects/spring-boot/issues/4711 这个issue提出不到20天给我搜出来了,还是相信google的强大 问题: ...

  3. linux文本操作界面 vi面板如何复制一行

    linux文本操作界面 vi面板如何复制一行 1)把光标移动到要复制的行上2)按yy3)把光标移动到要复制的位置4)按p 在vi里如何复制一行中间的几个字符?如果你要从光标处开始复制 4 个字符,则先 ...

  4. 如何通过js和jquery获取图片真实的宽度和高度

    什么时候需要获取图片真实的宽度和高度 在做pc网页的时候,有时候会考虑按照插入的图片的尺寸来判断图片是横图还是竖图.然后判断过后给予不同的展示方式! 另外一种就是在手机页面上,在新闻页插入的图片往往都 ...

  5. 李洪强iOS经典面试题128

    1.写一个NSString类的实现 + (id)initWithCString:(c*****t char *)nullTerminatedCString encoding:(NSStringEnco ...

  6. Android事件处理

    含义:为用户动作提供响应就是事件处理. Android提供了强大的事件处理机制:基于监听的事件处理.基于回调的事件处理. 一.基于监听的事件处理 监听的处理模型主要涉及三类对象 >Event S ...

  7. 使用explain查看mysql查询执行计划

    explain语句: 字段解释: type:     all(全表扫描)     ref() possible_keys:     预测使用什么列做为索引 key:     实际使用的key     ...

  8. sbt assembly build.sbt content

    // import sbt._ // import sbt.Keys._ // import java.io.File // import AssemblyKeys._ name := "n ...

  9. Linux关闭休眠和屏保模式

    本人因为特殊需求,想让某台Linux主机始终显示某个程序,显示器不能关机或者休眠或进入屏保模式. 环境:Ubuntu 11.10 最小化模式安装并安装有轻量级桌面openbox(非gnome).因为X ...

  10. HttpClient模拟http请求

    Http协议的重要性相信不用我多说了,HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性(具体区别,日后我们再讨论),它不仅是客户端发送Http请求变得容易,而且 ...