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代码:

 1 #include <cstdio>
 2 #define Mod 1000000007
 3
 4 int T, s, t;
 5 long long n, r;
 6
 7 inline int cal()
 8 {
 9     if(n == s) return 1;
10     t = n-s-1, r = s;
11     while(t > 0)
12     {
13         if(t&1) r=(r*n)%Mod;
14         t >>=1, n=(n*n)%Mod;
15     }
16     return r;
17 }
18
19 int main()
20 {
21     scanf("%d", &T);
22     while(T --)
23     {
24         scanf("%d%d", &n, &s);
25         printf("%d\n", cal());
26     }
27     return 0;
28 }

时间: 2024-10-10 17:51:20

zjuoj 3604 Tunnel Network的相关文章

搭建openstack之前的环境准备

Base 虚拟机的网卡配置:(连接互联网,下载需要的rpm包) eth0:桥接网络 192.168.0.10/根据自己的电脑的实际情况进行设置 ip:192.168.0.110 netmask:255.255.255.0 gateway:192.168.0.1 nds1:211.167.230.200 nds2:211.167.230.100 eth1:10.0.0.1/24 management network eth2:10.0.1.1/24 tunnel network eth3:192.

wpa_supplicant软件架构分析

wpa_supplicant软件架构分析 1. 启动命令 wpa supplicant 在启动时,启动命令可以带有很多参数,目前我们的启动命令如下: wpa_supplicant /system/bin/wpa_supplicant -Dwext -ieth0 -c/data/wifi/wpa_supplicant.conf -f/data/wifi/wpa_log.txt wpa_supplicant对于启动命令带的参数,用了两个数据结构来保存, 一个是 wpa_params, 另一个是wpa

centos7安装openstack-kilo(一)

OpenStack是一个开源的云计算管理平台项目,由几个主要的组件组合起来完成具体工作.OpenStack支持几乎所有类型的云环境,项目目标是提供实施简单.可大规模扩展.丰富.标准统一的云计算管理平台.OpenStack通过各种互补的服务提供了基础设施即服务(IaaS)的解决方案,每个服务提供API以进行集成. 一.openstack的组件 ①计算(Compute):Nova.一套控制器,用于为单个用户或使用群组管理虚拟机实例的整个生命周期,根据用户需求来提供虚拟服务.负责虚拟机创建.开机.关机

openstack-ansible Chapter 4. Deployment configuration

Initial environment configuration Copy the contents of the /opt/openstack-ansible/etc/openstack_deploy directory to the/etc/openstack_deploy directory. Change to the /etc/openstack_deploy directory. Copy the openstack_user_config.yml.example file to/

hostapd wpa_supplicant madwifi详细分析(八)——wpa_supplicant的配置文件

距离上一篇文章的更新已经将近半年了,这半年忙项目忙得几乎没有什么时间给自己积累一些东西,也没什么心思转到这边来写点东西,当一个项目放到自己身上的时候,发现并不像开发一个模块或一个功能那么简单,涉及方方面面,各种琐碎的的事情会占据大量的时间. 前面写的关于hostapd的文章都太浅显了,一般都没怎么涉及到具体的功能,只是简单的分析了一下代码的流程,然而对于实际的功能开发和bug调试其实用处不大,后面的文章将从wpa_supplicant入手做进一步的了解.hostapd主要用于AP端的开发,比如路

Networking in too much detail

The players This document describes the architecture that results from a particular OpenStack configuration, specifically: Quantum (or Neutron) networking using GRE tunnels; A dedicated network controller; A single instance running on a compute host

D02 TED Elon Mulsk The future we&#39;re building — and boring

摘要:精选TED. 每个音频不超过2分钟,学英语和吸收伟大思想两不误 音频: https://n1audio.hjfile.cn/st/de745862-d5f4-4ef4-9218-d79361ca8512.mp3  原文: 01:17EM: Yeah, absolutely. So this is the first time -- Just to show what we're talking about. So a couple of key things that are import

OpenStack总体架构概览&amp;OpenStack核心组件介绍

下面个是51CTO上一位朋友发布的O版OpenStack核心组件说明,总结的非常到位,所以我就不再造轮子了.~,~ https://down.51cto.com/data/2448945 私有云 公有云 混合云 IaaS(基础架构即服务):OpenStack,CloudStack PaaS(平台即服务):Docker,Openshift SaaS(服务即服务):主要面对终端用户,可通过一个浏览器就可以实现使用任何应用,而无需安装. DBaaS(Database as a Service) FWa

Linux 性能监控 : CPU 、Memory 、 IO 、Network

一.CPU 1.良好状态指标 CPU利用率:User Time <= 70%,System Time <= 35%,User Time + System Time <= 70% 上下文切换:与CPU利用率相关联,如果CPU利用率状态良好,大量的上下文切换也是可以接受的 可运行队列:每个处理器的可运行队列<=3个线程 2.监控工具 vmstat $ vmstat 1 procs -----------memory---------- ---swap-- -----io---- --s