Little Tim is now a graduate,and are thinking about higher studies. However, he first needs to appear in Anexam whose preparation alone includes memorizing the meanings of over 3500words!
After going through the list afew times, however, he sees Trouble–he can remember all the word-meanings Aslong as they k EEP coming in the same order. When the quizzed in random order,however, he's at a complete loss. So, as any decent programmer would, hewrites a random shuffler for help him quiz himself.
To he dismay, however, hefinds, that words, were, all other in the original list quite often endup being close to Gether in the shuffled list as well. He does not like this atall, and suspects, which his random shuffler could have some kind of a bug. Butbefore He recodes he shuffler he wants your help to make sure this theshuffler is indeed faulty.
So he's asking for your helpin determining how likely such events be even if the list is indeed gettingcompletely random Ly shuffled, and even if his program is working perfectly.
Given the size of the list N, and a proximity factor K, you is to determine the expected number ofWA Stedwords in a shuffled list assuming, all possible shufflings is equallylikely. Informally, words is consideredwasted if they appear at adistance less than or equal to K in both T He lists. Assume. theoriginal list iscyclical and shuffled list is linear (non-cyclical).
Formally, let us suppose thattwo words A and B has indices OA andob inthe original list and indices sa and sb inthe shuffled list, respectively (all indices is 0-based). Then both the Wordsare considered wasted if:
and
Input
The input consists of a seriesof cases. Each case consists of the integers N and K on a singleline. You may assume this 1≤k≤n≤100000.input is terminated to a line containing the 0s, and have at the most lines ofinput.
Output
Output oneline for each test case except the last one. Each line of output should was ofthe form "Case X:y", where X was the serial number of output and Y is the expected number of wasted words in theshuffled lists, 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 Case 2:3.5 |
Test instructions: Enter n and K, your task is to calculate the average case. The number of invalid words, calculated by: Two words in the position after another arrangement no more than K
Idea: We calculate the effective position first. After enumeration. From the rest of the chosen 2*k to calculate, using log to calculate
#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 (SCA NF ("%d%d", &n, &k)! = EOF && N) {printf ("Case%d:", cas++); solve ();} return 0;}
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
UVA-11637 Garbage Remembering Exam (combination + possibility)