HDU 2048: recursion & staggered probability

Source: Internet
Author: User
Tags time limit ticket

God, God, and Jesus Christ.

http://acm.hdu.edu.cn/showproblem.php?pid=2048

Time limit:2000/1000 MS (java/others)

Memory limit:65536/32768 K (java/others)

Problem Description

HDU 2006 ' ACM Contest Awards Party Grand start! In order to enliven the atmosphere, the organizers held an innovative, the prizes of the rich lottery, the specific requirements of this activity is: first of all, all the participants in the party will be a note of their own name into the lottery box; then, after all the notes have been added, each person takes a note from the box; If you get a note written on your own name, then "Congratulations, the jackpot!" "We can imagine the atmosphere at that time warm, after all, the winner of the prize is everyone's dream twins signature photo!" However, as all attempts to design comedy often end in tragedy, the lottery finally did not win a winner! My God, God, and Jesus Christ, how could this be? But don't get excited, now the question is, can you calculate the probability of this happening? Not counting? Do you want to end it with a tragedy?

Input

The first line of input data is an integer c that represents the number of test instances, then the C row data, each containing an integer n (1<n<=20), indicating the number of people participating in the lottery.

Output

For each test instance, export the percentage that occurs, one row for each instance, and two decimal digits (rounded), depending on the sample output.

Sample Input

12

Sample Output

50%

Ideas:

1. All possible permutations of the n note are naturally n! (denominator). The problem now is to find the wrong number f (n) (molecule) of the N note. First of all, we consider that if the front N-1 people are not their own ticket, that is, the former N-1 individual satisfied with the wrong row, now came a person, he was holding his own ticket. As long as he exchanges his ticket with any of the other N-1 individuals, he can satisfy the N-person's wrong row. There are (N-1) *f (N-1) methods.

Besides, we consider that if the former N-1 individual does not meet the wrong row, and the nth person to his own ticket with one of the exchange after the right row to meet. This happened in the original N-1, N-2 individual satisfied with the wrong platoon, there is only one person with his own ticket, and the nth person who happens to exchange with him, this time to meet the wrong row. And because of the first n-1 people, everyone has a chance to hold their own tickets. So there is the possibility of N-1 exchange. So there is (N-1) *f (N-2) method.

To sum up: f (N) = (N-1) *[f (N-1) +f (N-2)]

2. Another way to deduce the idea is to first calculate the error row f (n) =n! directly by the tolerance theorem (1/2!-1/3!+.....+ (( -1) ^n)/n!)

So f (n)/n!-f (n-1)/(N-1)!= (( -1) ^n)/n!

Multiply on both sides n! have f (n)-n*f (n-1) = ( -1) ^n

So f (n-1)-(n-1) *f (n-2) = ( -1) ^ (n-1) = ( -1) *[f (n)-n*f (n-1)]

Reduction of f (n) = (n-1) *[f (n-1) +f (n-2)]

3. Calculate by the simplified formula of the dislocation permutation number (refer to Wikipedia)

Complete code:

/*0ms,224kb*/
    
#include <cstdio>  
#include <cmath>  
const DOUBLE e = exp (1.0);  
    
Double p[21];  
    
int main ()  
{  
    Double temp = 1.0;  
    int T, n, I;  
    for (i = 2; I <= ++i)  
    {  
        temp *= i;  
        P[i] = round (temp/e)/temp;  
    }  
    scanf ("%d", &t);  
    while (t--)  
    {  
        scanf ("%d", &n);  
        printf ("%.2f%%\n", P[n] * 100.0);  
    return 0;  
}

This article URL address: http://www.bianceng.cn/Programming/sjjg/201410/45512.htm

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.