题解

题解 P4981父子 (Carley定理)

Posted on 2021-02-20,4 min read

原题链接

题意

构造一棵含n个带序号的点的有根树,求方案数

Carley定理~
Carley定理:n个带序号的节点共能构成nn−2n^{n-2}个形态不同的无根树

套!

Carley定理处理的是无根树,而这题是有根树;树的每种形态都有n个点可做根,所以总方案数为nn−1n^{n-1}

核心代码

int main()
{
	int t;
	read(t);
	F(i, 1, t)
	{
		ll n;
		read(n);
		write(qpow(n, n-1), '\n');
	}
	return 0;
}

全代码


// Problem: P4981 父子
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P4981
// Memory Limit: 128 MB
// Time Limit: 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

//#include <bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <cmath>
#include <algorithm>
#include <climits>
#include <functional>
#include <cstring>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <complex>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define itn int
#define nit int
#define ll long long
#define ms multiset
#define F(i, a, b) for(register int i = a, i##end = b; i <= i##end; ++i)
#define UF(i, a, b) for(register int i = a, i##end = b; i >= i##end; --i)
#define re register
#define ri re int
#define il inline
#define pii pair<int, int>
#define cp complex<double>
//#pra gma G CC opti mize(3)
using namespace std;
using std::bitset;
//using namespace __gnu_pbds;
const double Pi = acos(-1);
namespace fastIO
{
    template<class T>
    inline void read(T &x)
    {
        x = 0;
        bool fu = 0;
        char ch = 0;
        while(ch > '9' || ch < '0')
        {
            ch = getchar();
            if(ch == '-')
                fu = 1;
        }
        while(ch <= '9' && ch >= '0')
            x = (x * 10 - 48 + ch), ch = getchar();
        if(fu)
            x = -x;
    }
    inline int read()
    {
        int x = 0;
        bool fu = 0;
        char ch = 0;
        while(ch > '9' || ch < '0')
        {
            ch = getchar();
            if(ch == '-')
                fu = 1;
        }
        while(ch <= '9' && ch >= '0')
            x = (x * 10 - 48 + ch), ch = getchar();
        return fu ? -x : x;
    }
    char _n_u_m_[40];
    template<class T>
    inline void write(T x )
    {
        if(x == 0)
        {
            putchar('0');
            return;
        }
        T tmp = x > 0 ? x : -x;
        if(x < 0)
            putchar('-');
        register int cnt = 0;
        while(tmp > 0)
        {
            _n_u_m_[cnt++] = tmp % 10 + '0';

            tmp /= 10;
        }
        while(cnt > 0) 
            putchar(_n_u_m_[--cnt]);
    }
    template<class T>
    inline void write(T x ,char ch)
    {
        write(x);
        putchar(ch);
    }
}
using namespace fastIO;
ll mod = 1e9 + 9;

ll qpow(ll n, ll k)
{
	ll tmp = 1;
	while(k)
	{
		if(k & 1)
		{
			tmp *= n;
			tmp %= mod;
		}
		k >>= 1;
		n *= n;
		n %= mod;
	}
	return tmp;
}

int main()
{
	int t;
	read(t);
	F(i, 1, t)
	{
		ll n;
		read(n);
		write(qpow(n, n-1), '\n');
	}
	return 0;
}

下一篇: bugs→

▲