P4430 monkey fight, P4981 father and son

prufer coding

Of course, you can also be understood as Cayley formula, in fact, this formula is prufer will be able to step through the launch of coding

P4430 monkey fight
P4981 father and
maybe about the same topic


Let me talk about his son, it is clear that the subject is to make you beg \ (n \) points rooted tree has several
\ (n \) unrooted tree prufer coded points have \ (n-2 \) position, and tree-one correspondence and encoding each bit may be repeated and
then it \ (n ^ {n-2 } \) kinds of unrooted trees constructor
Therefore, let each node in turn as a root, so the answer is \ (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;
}

Monkey fight that problem:
a forest beginning there \ (N \) only know each other little monkeys, they often fight, but the fight must not be both a good friend.
After each frame is finished, the two sides fight, and their friends will know each other and become good friends. After \ (N-1 \) after the second fight of the forest monkey would become good friends
The question now is, a total of how many different fights process.
For example, when \ (N = 3 \) when there \ (\ {1-2 and 1-3 \} \ {1-2, 2-3 \} \ {1-3,1-2 \} , \ {1-3, 2-3 \} \ {2-3,1-2 \} \ {2-3,1-3 \} \) different process six kinds of fights.

This requirement is the question unrooted trees, but also count \ (n-1 \) different order edges to be added
so the answer is \ ((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;
}

Guess you like

Origin www.cnblogs.com/suxxsfe/p/12641846.html