Xinjiang University ACM-ICPC Programming Contest May Contest (Synchronized Contest) - Expectations of Monkey Sorting

Link: https://www.nowcoder.com/acm/contest/116/F
Source: Niuke.com

Topic description

We know that there is a magical sorting method called monkey sorting, which is to write the numbers to be sorted on the cards, then let the monkeys throw the cards in the air, and when they fall, observe whether the cards have been sorted from left to right (we think It will not happen that the cards are stacked together after landing) If the order is in order, the ordering is completed, otherwise let the monkey toss it again until the cards are in order, then the problem comes, give you N cards, each with a capital letter written on it Letters, what is the probability that the monkey will complete the lexicographical order the first time the monkey throws these cards?

Enter description:

The first line is an integer N (1<N<100) representing N cards for the monkey, followed by a string of length N representing the letters written on those cards.

Output description:

Output a line representing the probability that the monkey sort succeeds the first time (expressed as a score with a numerator of 1).
Example 1

enter

7
SCIENCE

output

1/1260 

question meaning: Chinese question;
idea: At the beginning, the result of using C++ was 33.33% and I thought the idea was wrong. Later, I found that the factorial of 100 would explode and use a large number, so I decisively opened the java
code:
import java.math.BigInteger;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
    Scanner cin=new Scanner(System.in);
    int n;
    BigInteger a,b;
    b=BigInteger.ONE;
    a=BigInteger.ONE;
    n = cin.nextInt ();
    int [] num = new  int [ 30 ];
    String str;
    str=cin.next();
    for(int i=0;i<30;i++)num[i]=0;
    for(int i=0;i<str.length();i++){
        num[str.charAt(i)-'A']++;
        //System.out.println(num[i]);
    }
    BigInteger ans = BigInteger.ONE;
    for(int i = 1; i <= n; i++)
    ans = ans.multiply(BigInteger.valueOf(i));
    for(int i=0;i<30;i++){
        BigInteger now = BigInteger.ONE;
        for(int j = 1; j <= num[i]; j++)
            now = now.multiply(BigInteger.valueOf(j));
        ans = ans.divide(now);
    }
    System.out.println("1/"+ans);
}
}

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325211699&siteId=291194637