Problem Description
The chef is having a dinner party. He has N chairs and has invited N guests. The chef knows that if the guests are left to their own devices, they tend to sit in the same chairs and socialize with the same people all night. To prevent this, the chef has developed a plan to help people socialize. He will assign each chair a follow-up chair. At predetermined intervals during the party, the chef will ring a bell, instructing all guests to move from their current chair to its follow-up chair.
The chef will assign follow-up chairs randomly, with the restriction that no chair will be its own follow-up chair, and no two chairs will have the same follow-up chair. That is, the chef randomly chooses one arrangement out of all assignments satisfying the two conditions. The chef wonders, after a certain number of ringings of the bell, what the expected number of guests who will be back in their original chairs will be.
Input
Input will begin with an integer T, the number of test cases. Each test case consists of a single line with 2 integers N and R, the number of chairs and number of ringings, respectively.
Output
For each test case, output on a single line the expected number of guests who will be back in their original seats after exactly R ringings, rounded to 5 decimal places.CODING ARENA
#include <stdio.h>
int main()
{
int fall,n,r,i=1;
long double a,b[51],c[51];
for(c[1]=!(b[0]=b[1]=c[0]=1);++i<=50;b[i]=i*b[i-1],c[i]=(i-1)*(c[i-1]+c[i-2]));
for(scanf("%d",&fall);fall--;printf("%.5Lf\n",(a/c[n]*n)))
for(i=!(a=!scanf("%d%d",&n,&r));++i<=n;a+=(!(r%i))?b[n-1]/b[n-i]*c[n-i]:0);
return 0;
}
Test Case 1
Input (stdin)4 2 1 2 2 4 2 5 3
Expected Output0.00000 2.00000 1.33333 1.36364
Test Case 2
Input (stdin)5 2 1 2 2 4 2 5 3 6 7
Expected Output0.00000 2.00000 1.33333 1.36364 0.00000
No comments:
Post a Comment