[BZOJ1005] [HNOI2008] Clearly the trouble

Source: Internet
Author: User

[BZOJ1005] [HNOI2008] Clearly the trouble

Question Description

Ever since I learned the structure of a tree, I have been interested in strange trees ... Gives the point labeled 1 to N, and the final degree of some points, allowing
Any two-point connection, how many trees can be produced to meet the requirements?

Input

The first behavior n (0 < n < = 1000),
Next n rows, the i+1 line gives the degree di of the I node, and if not required for the degree, enter-1

Output

An integer that represents the number of different trees that satisfy the requirement, no output 0

Input example

3 1-1-1

Output example

2

Data size and conventions

See " Input "

Exercises

The problem with knowing the prufer sequence is to delete the problem. This problem not only to write high-precision, but also can not use division, hard pressure constant, combined number to decompose factorization before!!

= = Just practice high precision.

#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <stack > #include <vector> #include <queue> #include <cstring> #include <string> #include <map > #include <set>using namespace std;const int buffersize = 1 << 16;char buffer[buffersize], *head, *TAIL;INL        ine Char Getchar () {if (Head = = Tail) {int L = fread (buffer, 1, buffersize, stdin);    Tail = (Head = buffer) + L; } return *head++;}    int read () {int x = 0, f = 1; char c = Getchar ();    while (!isdigit (c)) {if (c = = '-') f =-1; c = Getchar ();}    while (IsDigit (c)) {x = x * + C-' 0 '; c = Getchar ();} return x * f;} #define MAXN 1010int N, Deg[maxn], cnt;struct bign {int len, a[5010];bign () {len = 1; memset (A, 0, sizeof (a));} Bign operator = (const int& t) {len = 1; A[0] = T;while (a[len-1] > 9) A[len] = a[len-1]/ten, a[len-1]%=, Len++;return *this;} void Clear () {for (;! A[len-1] && Len; len--); IF (!len) len = 1;return;} Bign operator * (const int& t) const {bign ans; ans.len = len;memcpy (ans. A, a, sizeof (a)), for (int i = 0; i < len; i++) ans. A[i] *= t;for (int i = 0; i < len; i++) ans. A[i+1] + = ans. A[i]/ten, ans. A[i]%= 10;int j = len + 1;for (; ans. A[J-1] > 9;) ans. A[j] + = ans. A[J-1]/ten, ans. A[j-1]%=, J++;ans.len = J;ans.clear (); return ans;} Bign operator *= (const int& t) {*this = *this * T;return *this;} Bign operator * (const bign& t) const {bign ans; ans.len = len + t.len-1;for (int i = 0; i < len; i++) for (int j = 0; J < T.len; J + +) ans. A[I+J] + = a[i] * t.a[j];for (int i = 0; i < Ans.len; i++) ans. A[i+1] + = ans. A[i]/ten, ans. A[i]%= 10;int j = ans.len + 1;for (; ans. A[J-1] > 9;) ans. A[j] + = ans. A[J-1]/ten, ans. A[j-1]%=, J++;ans.len = J;ans.clear (); return ans;} Bign operator *= (const bign& t) {*this = *this * T;return *this;} Bign operator-(const bign& t) const {bign ans; ans.len = len;memcpy (ans. A, a, sizeof (a)); for (int i = 0; i < Len; i++) {if (I < t.len) ans. A[i]-= t.a[i];if (ans. A[i] < 0) ans. A[i] + = ten, ans. a[i+1]--;} while (!ans. A[ans.len-1]) Ans.len--;return ans;} Bign operator-= (const bign &t) {*this = *this-t;return *this;} Bign operator/(const bign& t) const {bign ans, f; f = 0; Ans.len = -1;for (int i = len-1; I >= 0; i--) {f *= 10; F.a[0] = A[i];while (f >= t) {f-= T;ans. A[i]++;if (Ans.len = =-1) Ans.len = i + 1;}} return ans;}  BOOL operator >= (const bign& t) const {if (len! = T.len) return len > t.len;for (int i = len-1; I >= 0; i--) if (a[i]! = T.a[i]) return a[i] > T.a[i];return 1;} void print () {for (int i = len-1; I >= 0; i--) Putchar (a[i] + ' 0 '); return;}} ; int PRIME[MAXN], cp;bool vis[maxn];void prime_table () {for (int i = 2; I <= n; i++) {if (!vis[i]) PRIME[++CP] = I;for (in T j = 1; J <= CP && I * prime[j] <= N; J + +) {Vis[i*prime[j]] = 1;if (i% prime[j] = = 0) break;}} return;} Bign Pow (int a, int n) {bign sum, t; sum = 1; t = A;while(n) {if (n & 1) sum *= t;t *= t; n >>= 1;} return sum;} int cp[maxn];bign C (int n, int m) {for (int i = 1; I <= Cp; i++) Cp[i] = 0;for (int i = n; I >= n-m + 1; i--) {int T MP = i;for (int j = 1; J <= CP; J + +) if (tmp% prime[j] = = 0) while (tmp% prime[j] = 0) cp[j]++, tmp/= PRIME[J];} for (int i = m; i; i--) {int tmp = i;for (int j = 1; J <= CP; J + +) if (tmp% prime[j] = = 0) while (tmp% prime[j] = = 0) cp[j ]--, tmp/= PRIME[J];} Bign sum; sum = 1;for (int i = 1; I <= CP; i++) if (cp[i]) sum *= Pow (Prime[i], cp[i]); return sum;} int main () {n = read ();p rime_table (), int tot = 0;bool OK = 1;for (int i = 1; I <= n; i++) {int x = read (); if (x >= 0) DEG[++CNT] = x-1, tot + = (x-1); if (!x) ok = 0;} if (!ok | | tot > N-2) return puts ("0"), 0;tot = N-2;bign sum; sum = 1;for (int i = 1; I <= cnt; i++) {sum *= C (tot, deg[i]); tot-= Deg[i];} Sum *= Pow (n-cnt, tot); Sum.print (); Putchar (' \ n '); return 0;}

The definition of division in fact useless, I first used a division T flew, now lazy to delete.

[BZOJ1005] [HNOI2008] Clearly the trouble

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.