You are on page 1of 2

<a href="http://www.codechef.

com/APRIL15/problems/DEVVOTE">Problem Description</
a>
I know two standard ways of calculate the expectation of a random variable X:
1. Use directly the definition of expected value i.e for each event multiply it'
s occurance probability by the value that the Random variable assigns to that ev
ent.
2. Decompose the random variable in a sum of random variables, and then take adv
antage of the linear behaviour of the expected value i.e $latex E(X_1+X_2+..+X_n
)=E(X_1)+E(X_2)+...+E(X_n) $
In this problem it was enough to use the first approach.
We can use dynamic programming for calculate the expected value of the number of
presidents if we consider only the first i Devus.
So our goal is to calculate the answer for the first i Devus, if we already know
the answer for the first (i-1) Devus. However, we also need some some informati
on about the decisions of the previous (i-1) Devus.
A natural idea to compress the information about the previous decisions is to ke
ep just the number of devus with k votes for each k.
We also need to know which was the decision of the devu (i-1) - Remember that tw
o adjacent devus can't select the same devu With that in mind it's easy to come up with an algorithm for solving the problem
. [Details omited:)]
Ps 1. I think using the second approach it's possible to solve the problem: We j
ust need to calculate the probability that the ith devu becomes a president.
Ps 2. An useful way of thinking about the decisions of the devus is using an arr
ay of numbers e.g 1 2 1 3 1 means that the first devu voted by devu 1, the secon
d by devu 2, the third by devu 1, ...
[code language="cpp"]
#include<bits/stdc++.h>
using namespace std;
typedef long long int uli;
int n;
map<pair<vector<int>,int>,double>f;
double solve(vector<int>v,int lst){
pair<vector<int>,int>ky(v,lst);
if(f.count(ky))return f[ky];
int ix=0;
for(int i=0;i<=n;i++) ix+=v[i]*i;
if(ix==n){
for(int i=n;i>=0;i--)
if(v[i]>0) return v[i];
}
double ans=0.0;
double tot=n;
if(lst!=-1)tot--;
for(int i=0;i<n;i++){
if(v[i]>0){
double q=v[i];
if(i==lst)q--;
v[i]--;
v[i+1]++;
ans+=q*solve(v,i+1);

v[i]++;
v[i+1]--;
}
}
ans/=tot;
f[ky]=ans;
return ans;
}
int main(){
//
freopen("data.in","r",stdin);
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
vector<int>v(n+1,0);
v[0]=n;
double ans=solve(v,-1);
printf("%.14lf,",ans);
}
return 0;
}
[/code]

You might also like