*【ZOJ - 3604】Tunnel Network (Cayley定理,purfer数列,无根树定理,构造,结论,或dp)

题干:

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

解题报告:

 且不说会不会T的问题,这个思路应该是没错的,但是只能过小样例,过不了大样例,不知道为什么。

 

AC代码:

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

const int mod=1e9+7;

int main() {
	int T;
	scanf("%d",&T);
	while(T--) {
		ll n,s;
		scanf("%lld %lld",&n,&s);
		ll ans=s;
		for(ll i=1; i<n-s; i++) {
			ans=(ans*n)%mod;
		}
		if(n==s)ans=1;
		printf("%lld\n",ans);
	}
	return 0;
}

WA代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 2e5 + 5;
const ll mod = 1000000007;
int n,s;
ll C[2005][2005];
ll dp[2005][2005];
ll qpow(ll a,ll k) {
	if(k<=0) return 1;
	ll res = 1;
	while(k) {
		if(k&1) res = (res * a) % mod;
		a=(a*a)%mod;
		k>>=1;
	}
	return res;
}
void init() {
	C[0][0] = 1;
	for(int i = 1; i<=2002; i++) {
		C[i][0] = 1;
		for(int j = 1; j<=2002; j++) {
			C[i][j] = C[i-1][j] + C[i-1][j-1];
		}
	}
}
int main()
{
	init();
	int t;
	cin>>t;
//	dp[i][j]代表前i个集合中放入了j个点的方案数。
	while(t--) {
		scanf("%d%d",&n,&s);
		memset(dp,0,sizeof dp);
		dp[0][0]=1;
		for(int i = 0; i<=n-s; i++) {
			dp[1][i] = (C[n-s][i]*qpow(i+1,i-1))%mod;
		}
		for(int i = 2; i<=s; i++) {
			for(int j = 0; j<=n-s; j++) {
				for(int k = 0; k<=j; k++) {
					dp[i][j] = (dp[i][j] + C[n-s-(j-k)][k] * dp[i-1][j-k]*qpow(k+1,k-1))%mod;
				}
			}
		}
		ll ans = dp[s][n-s];
//		for(int i = 1; i<=n-s; i++) {
//			ans = (ans + dp[s][i])%mod;
//		}
		printf("%lld\n",ans);
	}

	return 0 ;
}
/*
100
4 1


*/

https://blog.csdn.net/qq_36424540/article/details/80043433

全部评论

相关推荐

SHC2:关键问题是你这三段实习是三个不同的岗位…你这样子秋招就是只有一段实习的本科生..
点赞 评论 收藏
分享
04-17 10:16
门头沟学院 Java
小浪_coder:24届很难找了,马上25的都毕业了还有很多没找到的
点赞 评论 收藏
分享
繁华的街道两旁,湿漉漉的下午,两个青涩的脸庞互相张望。宽大卫衣下娇小的她,向我奔来。不约而同的卫衣,斯文的半框眼镜掩饰着一个穷臭屌丝气息。这是我和我牛爱网第一死忠粉兼专属女嘉宾最初的见面。火速恋爱,但是没有所谓的快节奏,相识半年,还是一样的热恋。吃着肉夹馍坐过西安的小三轮洱海边自行车的气球胖吃着她最喜欢的酸酸水果和小乳扇在南山某店爷爷穿孙子衣服,摸肥猫就算我在忙也要抽出时间陪她去吃他喜欢的漂亮饭生活总是平凡,但平凡不平淡还记得见面第一件事儿:“我去上个厕所。”现在早上第一件事儿:“拉*”第一次上我车的她:“我可以坐副驾吗?”现在的她:“老子把jio翘到上面得得挡到你后视镜。”这小孩,虽然花了我...
Stan_蹒跚者:确很厉害,但是有一个小问题:谁问你了?我的意思是,谁在意?我告诉你,根本没人问你,在我们之中0人问了你,我把所有问你的人都请来 party 了,到场人数是0个人,誰问你了?WHO ASKED?谁问汝矣?誰があなたに聞きましたか?누가 물어봤어?我爬上了珠穆朗玛峰也没找到谁问你了,我刚刚潜入了世界上最大的射电望远镜也没开到那个问你的人的盒,在找到谁问你之前我连癌症的解药都发明了出来,我开了最大距离渲染也没找到谁问你了我活在这个被辐射蹂躏了多年的破碎世界的坟墓里目睹全球核战争把人类文明毁灭也没见到谁问你了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务