UVA 11637 Garbage Remembering Exam
#include <iostream> #include <stdio.h> #include <cstring> #include <math.h> #define MAX 100010 using namespace std; int n,k; long double fac[MAX]; void GetFacs() { for(int i=1;i<MAX;i++) { fac[i] = fac[i-1] + log((long double)i); } } double ExpectedValue() { int x; double p,e = n; if(n==1) return 0; for(int i=1; i<=n; i++) { x = max(i-k-1,0) + max(n-i-k,0); if(x-2*k>=0) { p = fac[x]+fac[n-2*k-1]-fac[n-1]-fac[x-2*k]; e -= exp(p); } } return e; } int main() { int t=0; GetFacs(); while(scanf("%d%d",&n,&k)!=EOF && !(n==0 && k==0)) { printf("Case %d: %.4lf\n", ++t, ExpectedValue()); } return 0; }