Luogu2290 [HNOI2004] tree count (count combination, prufer coding)

This is not prufer coding it, explosion-proof long long on the line ah

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long

#define ON_DEBUG

#ifdef ON_DEBUG

#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x)  cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);

#else

#define D_e_Line ;
#define D_e(x)  ;
#define Pause() ;
#define FileOpen() ;

#endif

struct ios{
    template<typename ATP>ios& operator >> (ATP &x){
        x = 0; int f = 1; char c;
        for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
        while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
        x*= f;
        return *this;
    }
}io;
using namespace std;

const int N = 157;

#define int long long
int C[N][N];
inline void Prepare(int &n){
    R(i,0,n){
        C[i][0] = 1;
        R(j,1,i){
            C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
        }
    }
}
int d[N];
#undef int
int main(){
#define int long long
    int n;
    io >> n;
    int sum = 0;
    if(n == 1){
        io >> d[1];
        if(!d[1]){
            printf("1");
        }
        else{
            printf("0");
        }
        return 0;
    }
    R(i,1,n){
        io >> d[i];
        if(!d[i]){
            printf("0");
            return 0;
        }
        --d[i];
        sum += d[i];
    }
    
    if(sum != n - 2){
        printf("0");
        return 0;
    }
    
    Prepare(n);
    
    sum = 0;
    int ans = 1;
    R(i,1,n){
        ans *= C[n - 2 - sum][d[i]];
        sum += d[i];
    }
    
    printf("%lld", ans);
    
    return 0;
}

Guess you like

Origin www.cnblogs.com/bingoyes/p/11258439.html