Unlocking Magic II Harbin University of Technology Fifth annual ACM Program design Contest

Source: Internet
Author: User



Law: a[i][j]= 1/i * a[i-1][j-1] + (i-1)/I * a[i-1][j];
(probability of using j-1 magic when one box is missing) (The probability of using the J Magic when one box is missing)

The formula is deduced as follows:

set A[I][J] to open the box I just need a J magic case.

1->1


Ii
1->1, 2->2; Twice
1->2, 2->1; A



1->1, 2->2, 3->3; three times
1->2, 2->1, 3->3; Two times 3->3 (N->n) is considered separately (becauseNeed to use magic once) contains(n->n)The probability of a situation occurring is1/n,
So for example a[3][2] = 1/n *a[2][1] (2 boxes with 1 magic cases < plus the current time using 2 magic >) + .... (not considered first)
1->3, 2->1, 3->2; A
1->1, 2->3, 3->2; Two times careful observation will find that when the 3->2 is 1 times 2 times and 1 times in the ① 2 times the same 3->1.


1->3, 2->2, 3->1; Two-on-the-same .... This is the case, and thisdoes not contain n->nThe probability will be(n-1)/nSo for (n-1)/n * a[i-1][j] (the probability of using J magic when one box is less)

1->2, 2->3, 3->1; A


#include <cstdio>using namespace std;double a[5001][5001];   I didn't know if I could open spicy. ~ ~ ~ (>_<) ~ ~ ~ int main () {    int n, K;    Double ans;       for (int i=1; i<=5000; i++)  a[i][1]=1.0/i;    for (int i=2, i<=5000; i++) {for        (int j=2; j<=i; J + +) {            if (i==j)    a[i][j] = a[i-1][j-1]*1.0/i;            else        a[i][j] = 1.0/i*a[i-1][j-1]+ (i-1) *1.0/i*a[i-1][j];        }    }    while (scanf ("%d%d", &n, &k)!=-1) {        ans = 0.0;        for (int i=1; i<=k; i++)            ans + = a[n][i];        printf ("%.4f\n", ans);    }    return 0;}

Unlocking Magic II
Time limit:3000 MS Memory limit:256000 K
Total Submit:6 (4 users) Total Accepted:3 (3 users)Rating:Special Judge:no
Description
Day, Triksi came to town to perform magic.
One of the programs is the lock charm: There are n boxes on the stage, a key in each box, and only one key to open it for each box. Triksi can open a box with magic and violence. But Triksi can only use the K-magic at most, asking what the probability of opening all the boxes within the K-Magic.
Input
Multiple sets of test data.
The first row of each group of data has an integer n and an integer k. (1 <= k <= n?<= 5000)
Output
For each set of data, the output line represents the corresponding answer. The result retains four decimal places.
Sample Input
3 1
3 2
3 3
10 3
Sample Output
0.3333
0.8333
1.0000
0.7061
Source
The fifth annual ACM Program Design Contest of Harbin Polytechnic University

Unlocking Magic II Harbin University of Technology Fifth annual ACM Program design Contest

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.