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. TC SRM 591

    表示第一次在div1,我要记录一下... 木有挂0,第一题不怎么难的,读题读了20分钟,又想了20分钟,时间有点长,然后各种小心,然后得了140分.... 后两个题,根本木有看,貌似做出来的也不多.. ...

  2. Nginx_查看并发连接数

    通过查看Nginx的并发连接,我们可以更清除的知道网站的负载情况.Nginx并发查看有两种方法(之所以这么说,是因为笔者只知道两种),一种是通过 web界面,一种是通过命令,web查看要比命令查看显示 ...

  3. Oracle connect by 树查询之二

    先用scott用户下的emp表做实验.emp表有个字段,一个是empno(员工编号),另一个是mgr(上级经理编号)下面是表中所有数据 1 select * from emp start with e ...

  4. Linux任务调度命令(轻松管理Linux)

    Linux任务调度其实就是让系统在某个时间执行某些命令或者程序,这样可以让管理员更加轻松地管理自己的Linux,当我刚了解到这个方法时,我的内心充满了无尽的欣喜,感觉Linux实在是太强大了. 下面我 ...

  5. javascrit2.0完全参考手册(第二版) 第2章第3节 变量

    变量存储数据.每个变量都有一个名字,叫做标识符.在js中声明变量使用var关键字,var为新的数据分配存储空间,或者指示一直标识符正在使用.声明变量非常简单: var x; 这个语句告诉解释器一个新的 ...

  6. 【Go语言】连接数据库SQLite、MySQL、Oracle

    本文目录 1.Go连接SQLite 1_1.SQLite推荐驱动 1_2.SQLite连接示例代码 2.Go连接MySQL 2_1.MySQL推荐驱动 2_2.MySQL连接示例代码 3.Go连接Or ...

  7. LaTex Font Size 字体大小命令

    LaTex中字体大小有很多中等级,分别由下列命令控制: \tiny \scriptsize \footnotesize \small \normalsize \large \Large \LARGE ...

  8. Sqoop_ 简单介绍

    一.基本作用 概念: Sqoop被称为协作框架,是在Hadoop.2.X生态系统的辅助型框架,简单说,就是一个数据转换工具,类似的协作框架有文件收集库框架Flume,任务协调框架Oozie,大数据We ...

  9. Linux_MySql安装

    1.卸载原始mysql-lib sudo rpm -e --nodeps mysql-libs-xx 2.yum安装mysql-server sudo yum -y install mysql -se ...

  10. 第2章 使用JavaScript

    学习要点: 1.创建一张HTML页面 2.<Script>标签解析 3.JS代码嵌入的一些问题