P4981 父子 Cayley公式

原文链接: http://www.cnblogs.com/Willems/p/11016727.html


CayleyCayley公式的定义是这样的,对于n个不同的节点,能够组成的无根树(原来是无向连通图或者是有标志节点的树)的种数是n^(n-2)种。(这里让大家好理解一点,就写成了无根树,其实应该是一样的概念)

那么我们的初步问题就解决了,接下来就是解决无根树和有根树之间的转换。

但是转换很难吗?把有根树转换成根节点有nn种情况的无根树,也就是n^(n-2) * n,化简就是n^(n-1)。答案也就是这个玩意了。

因为这道题,n比较大,所以就用一下快速幂。

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define LL long long
#define ULL unsigned long long
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define dep(i,j,k) for(int i=k;i>=j;i--)
#define INF 0x3f3f3f3f
#define mem(i,j) memset(i,j,sizeof(i))
#define make(i,j) make_pair(i,j)
#define pb push_back
using namespace std;
const int mod = 1e9 + 9;
LL ksm(LL a, LL b) {
    LL ans = 1;
    while(b) {
        if(b & 1) ans = ans * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ans;
}
int main() {
    int n, t;
    scanf("%d", &t);
    while(t--) {
        scanf("%d", &n);
        cout << ksm(1LL * n, 1LL * ( n - 1)) <<endl;
    }
    return 0;
}
View Code

转载于:https://www.cnblogs.com/Willems/p/11016727.html

猜你喜欢

转载自blog.csdn.net/weixin_30662539/article/details/95078834