P4981 父子(Cayley定理)

P4981 父子(Cayley定理)

题目传送门

思路:显然是用 C a y l e y Cayley 定理易得,n个结点有标号有根树的个数为: n n 1 n^{n-1} 种。。接下来写下快速幂就行了。

时间复杂度: O ( l o g n ) O(logn)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+9;
ll ksm(ll a,ll n){
    ll ans=1;
    while(n){
        if(n&1) ans=ans*a%mod;
        a=a*a%mod;
        n>>=1;
    }
    return ans;
}
int main(){
    ll t,n;
    scanf("%lld",&t);
    while(t--){
        scanf("%lld",&n);
        printf("%lld\n",ksm(n,n-1));
    }
    return 0;
}
原创文章 201 获赞 165 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_45750972/article/details/106074360