Sha-11637 garbage remembering exam (combination + probability)

Source: Internet
Author: User

 

Little Tim is now a graduate, and is thinking about higher studies. However, he first needs to appear in anexam whose preparation alone provided des memorizing the meanings of over 3500 words!

After going through the list afew times, however, he sees trouble-he can remember all the word-meanings aslong as they keep coming in the same order. when quizzed in random order, however, he is at a complete loss. so, as any decent programmer wocould, hewrites a random shuffler to help him quiz himself.

To his dismay, however, hefinds that words that were near each other in the original list quite often endup being close together in the shuffled list as well. he does not like this atall, and suspects that his random shuffler may have some kind of a bug. butbefore he recodes his shuffler he wants your help to make sure that theshuffler is indeed faulty.

So he is asking for your helpin determining how likely such events are even if the list is indeed gettingcompletely randomly shuffled, And even if his program is working perfectly.

 

Given the size of the ListN, And a proximity factorK, You are to determine the expected numberWastedwordsIn a shuffled list assuming that all possible shufflings are running lylikely. Informally, two words are consideredWastedIf they appear at adistance less than or equalKIn both the lists. Assume that theoriginal list isLogical iCalAnd shuffled list isLinear(Non-logical ical ).

Formally, let us suppose thattwo words A and B have indicesOAAndObInthe original list and indicesSAAndSbInthe shuffled list, respectively (all indices are 0-based). Then both the wordsare consideredWastedIf:

And

 

Input

The input consists of a seriesof cases. Each case consists of two integersNAndKOn a singleline. You may assume that 1 ≤ k ≤ n ≤ 100000. input is terminated by a line containing two 0 s, and has at most 125 lines ofinput.

 

Output

Output oneline for each test case when t the last one. each line of output shocould be ofthe form "case X: Y", where X is the serial number of output and Y is the expected number of wasted words in theshuffled list, printed with exactly four digits after the decimal point, withrounding if necessary.

 

Sampleinput outputfor sample input

5 2

5 1

0 0

Case 1: 5.0000

Case 2: 3.5000

 

Enter N and K. Your task is to calculate the number of invalid words on average. The calculation method is as follows: the positions of the two words cannot exceed K.

Idea: Calculate the valid position first. After enumeration, select 2 * k for calculation from the rest, and use log for calculation.

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const int maxn = 100005;int n, k;long double f[maxn];void solve() {if (n == 1) {printf("0.0000\n");return;}if (n <= 2 * k + 1) {printf("%d.0000\n", n);return;}int N = k << 1, p;long double sum = 0;for (int i = 1; i <= n; i++) {p = max(i-1-k, 0) + max(n-k-i, 0);if (p < N)continue;sum += exp(f[p] + f[n - N - 1] - f[n - 1] - f[p - N]);}printf("%.4lf\n", (double)(n - sum));}int main() {f[0] = f[1] = 0;for (int i = 2; i <= maxn; i++)f[i] = f[i-1] + log((long double) i); int cas = 1;while (scanf("%d%d", &n, &k) != EOF && n) {printf("Case %d: ", cas++);solve();}return 0;}







Sha-11637 garbage remembering exam (combination + probability)

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.