P4430 小猴打架、P4981 父子
作者:互联网
prufer编码
当然你也可以理解为 Cayley 公式,其实这个公式就是prufer编码经过一步就能推出的
P4430 小猴打架
P4981 父子
这俩题差不多
先说父子,很显然题目就是让你求\(n\)个点的有根树有几条
\(n\)个点的无根树的 prufer 编码有\(n-2\)位,且编码和树一一对应并且每一位可以重复
那么就有\(n^{n-2}\)种构造无根树的方法
所以,就让每一个节点轮流当根,所以答案就是\(n^{n-2}\times n=n^{n-1}\)
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<iomanip>
#include<cstring>
#define reg register
#define EN std::puts("")
#define LL long long
inline int read(){
register int x=0;register int y=1;
register char c=std::getchar();
while(c<'0'||c>'9'){if(c=='-') y=0;c=std::getchar();}
while(c>='0'&&c<='9'){x=x*10+(c^48);c=std::getchar();}
return y?x:-x;
}
inline LL power(LL a,LL b,LL mod){
LL ret=1;
while(b){
if(b&1) ret=ret*a%mod;
a=a*a%mod;b>>=1;
}
return ret;
}
int main(){int T=read();while(T--){
int n=read();
std::printf("%lld\n",power(n,n-1,1e9+9));
}
return 0;
}
小猴打架那题:
一开始森林里面有\(N\)只互不相识的小猴子,它们经常打架,但打架的双方都必须不是好朋友。
每次打完架后,打架的双方以及它们的好朋友就会互相认识,成为好朋友。经过\(N-1\)次打架之后,整个森林的小猴都会成为好朋友
现在的问题是,总共有多少种不同的打架过程。
比如当\(N=3\)时,就有\(\{1-2,1-3\},\{1-2,2-3\},\{1-3,1-2\},\{1-3,2-3\},\{2-3,1-2\},\{2-3,1-3\}\)六种不同的打架过程。
这个题要求的是无根树,但是还要算上\(n-1\)条边被加入的不同顺序
所以答案就是\((n-1)!\times n^{n-2}\)
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<iomanip>
#include<cstring>
#define reg register
#define EN std::puts("")
#define LL long long
inline int read(){
register int x=0;register int y=1;
register char c=std::getchar();
while(c<'0'||c>'9'){if(c=='-') y=0;c=std::getchar();}
while(c>='0'&&c<='9'){x=x*10+(c^48);c=std::getchar();}
return y?x:-x;
}
inline LL power(LL a,LL b,LL mod){
LL ret=1;
while(b){
if(b&1) ret=ret*a%mod;
a=a*a%mod;b>>=1;
}
return ret;
}
int main(){
int n=read();
LL ans=1;
for(reg int i=1;i<n;i++) ans=ans*i%9999991;
std::printf("%lld\n",ans*power(n,n-2,9999991)%9999991);
return 0;
}
标签:std,P4981,include,P4430,int,register,打架,小猴,define 来源: https://www.cnblogs.com/suxxsfe/p/12641846.html