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. 【CodeVS】p1299 切水果

    题目描述 Description 简单的说,一共N个水果排成一排,切M次,每次切[L,R]区间的所有水果(可能有的水果被重复切),每切完一次输出剩下水果数量 数据已重新装配,不会出现OLE错误 时限和 ...

  2. man page分類與說明

    轉載自http://itzone.hk/article/article.php?aid=200407152225014657 (如有侵權,請留言或來信告知) 前言 Man page是每位程式設計員及U ...

  3. ArcGIS 设置地图显示范围大小(全屏显示)

    Arcmap的FullExtent默认是地图加载的时候的extent.其实这个fullExtent是可以设置的. 打开ArcMap,选择左边图例的Layers ,右键点击,选择“Properties. ...

  4. 在DataGridView控件中加入ComboBox下拉列表框的实现

    在DataGridView控件中加入ComboBox下拉列表框的实现 转自:http://www.cnblogs.com/luqingfei/archive/2007/03/28/691372.htm ...

  5. 全浏览器收藏网站javascript

    function MyFavorite(sURL, sTitle) { var ctrl = (navigator.userAgent.toLowerCase()).indexOf('mac') != ...

  6. 利用MetaWeblog API实现XMLRPC写博客功能

    Windows Live Writer是一款小巧的写博客的工具,非常方便,甚至网上看到过有的评论称Live Writer是一款最不像微软产品的微软产品. Writer支持MSN Spaces以及Wor ...

  7. ubuntu安装Lua

    1.网站下载LUA包 curl -R -O http://www.lua.org/ftp/lua-5.2.3.tar.gz 2.下载ubuntu的编译支持sudo apt-get install bu ...

  8. c#选择填空题题库

    http://wenku.baidu.com/link?url=0g2mfcX_atcRIhJRqJnXzT1s2AIY-a2nR7pUguJn8cdSoy6V0CATevid3eQ7l-kgIDB6 ...

  9. Oracle索引简单介绍与示例

    索引的三大特性 1索引高度 在SQL检索数据(SELECT)的时候,索引的高度的不同对检索的效率有明显的差别,数据库访问索引需要读取的数据块通常是索引的高度+1个数据块数,也就是说索引的高度越高,访问 ...

  10. Hibernate SQLQuery简单实用,做链接查询

    工单里面可能有0个告警,一个或多个告警,当工单中没有告警的时候也需要将工单显示出来,所以就需要使用工单和告警的做链接查询,下面是具体实例 表: CREATE TABLE `alarm` ( `id` ...