[BZOJ-1005 & Luo Gu P2624] [HNOI2008] obviously troubles - [sequence] py + java Purfer

Topic links: https://www.lydsy.com/JudgeOnline/problem.php?id=1005
Luo Gu: https://www.luogu.com.cn/problem/P2624
Time Limit: 1 Sec  Memory Limit: 162 MB
Luo Gu: Time limit 1.00s  memory limit 125.00MB

Description

  Since obviously learned tree, the tree on the right strange generated interest ...... given numbered 1 to N points, and the final degree certain points, allowing
the connection between any two points may be how many degrees to meet the requirements of the tree was generated?

Input

  The first behavior N (0 <N <= 1000 ),
the next N rows, row i + 1-Di given degree of node i, if the degree is not required, enter -1

Output

  An integer representing the number of the tree meet different requirements, no solution outputs 0

Sample Input

3
1
-1
-1

Sample Output

2

HINT

  Two trees are 1-2-3; 1-3-2


 

Compared to bare Purfer sequence 2004, which is a question not so bare.

Consider k is known degree, then we can get the k known of answers depending on the nature Purfer sequence:

 So part of the answer should be:

 However, since there are n-2 purfer elements, then the sum of placing these elements has C (n-2, sum) species, i.e., the portion of the final answer:

 Then is the rest of the remaining vertices are nk, spare sequence n-2-sum positions, each vertex has n-2-sum of the discharge method, that is

Kind of answer.

Then the final answer will come out:

 

 emmmm, or py + JAVA wave wave it. .

The following is the AC Code: (Python)

n=int(input())
a=[]
for i in range(n):
    a.append(int(input()))
sum=0;k=0
for i in range(n):
    if a[i]!=-1:
        sum+=a[i]-1
        k+=1
    elif a[i]==0:
        print(0)
        exit()

c=1
for i in range(n-2-sum+1,n-2+1):
    c*=i
for i in range(2,sum+1):
    c//=i

tot=1
f=[1]
for i in range(1,sum+1):
    all * = and
    f.append(f[i-1]*i)
for i in range(n):
    if a[i]!=-1:
        all // = f [a [i] -1 ]

last=1;
for i in range(n-2-sum):
    last*=(n-k)

print (c * to * load)

 

(Java code):

import java.math.BigInteger;
import java.util.Scanner;

class Main {
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int a[]=new int[1005];
        int mark=0,sum=0,k=0;
        for (int i=1; i<=n; i++){
            a[i]=sc.nextInt();
            if (a[i]==0) mark=1;
            if (a[i]!=-1) {
                sum+=a[i]-1;k++;
            }
        }
        if (mark==1) {
            System.out.println("0");
            System.exit(0);
        }
        BigInteger s=new BigInteger("1");
        BigInteger num[]=new BigInteger[1005];
        num[0]=new BigInteger("0");
        int mx=n;
        if (sum>n) mx=sum;
        for (int i=1; i<=mx; i++)
            num[i]=num[i-1].add(new BigInteger("1"));
        for (int i=n-2; i>=n-2-sum+1; i--)
            s=s.multiply(num[i]);
        for (int i=2; i<=sum; i++)
            s=s.divide(num[i]);
        BigInteger tot=new BigInteger("1");
        for (int i=1; i<=sum; i++)
            all = tot.multiply (num [i]);
        BigInteger f[]=new BigInteger[1050];
        f[0]=new BigInteger("1");
        for (int i=1; i<=n; i++)
            f[i]=f[i-1].multiply(num[i]);
        BigInteger cnt=new BigInteger("1");
        for (int i=1; i<=n; i++)
            if (a[i]!=-1)  cnt=cnt.multiply(f[a[i]-1]);
        BigInteger last=new BigInteger("0");
        for (int i=1; i<=n-k; i++) last=last.add(new BigInteger("1"));
        BigInteger tot_last=new BigInteger("1");
        for (int i=1; i<=n-2-sum; i++) tot_last=tot_last.multiply(last);
        System.out.println(s.multiply(tot.divide(cnt)).multiply(tot_last));
    }
}

 

 

 

 

Guess you like

Origin www.cnblogs.com/lonely-wind-/p/12189938.html