Artifact Content
Not logged in

Artifact e7836058c4211ebead710b2f026b3472891c4ca5


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class CatsOnTheCircle { public:
	int N;
	double p;

	double getProb(int N_, int K, int p_)
	{
		N = N_;
		p = p_ / pow(10.0,9);
		return rec(false, 1, K-1);
	}
/*	
	void rec2(int V,
		vector<double>* p_t2e,
		vector<double>* q_t2e) {
		if(V == N-1) {
			p_t2e->assign(1, 1.0);
			q_t2e->assign(1, 1.0);
			return;
		}

		vector<double> pp, qq;
		rec2(V+1, &pp, &qq);

		vector<double> pp2;
		pp2 += (pp<<1) * rightOut(p,V);
		pp2 += rev(qq<<1) * (1-rightOut(p,V));
	}
*/
	double rec(bool inv, int V, int T) {
		if(V == N-1)
			return 1.0;
		double pp = 0.0;
		if(T!=0) pp += rightOut(inv?1-p:p,V) * rec(inv, V+1,T-1);
		if(T!=N-V-1) pp += (1-rightOut(inv?1-p:p,V)) * rec(!inv, V+1,N-V-2-T);
		return pp;
	}

	double rightOut(const double p, int V) {
		if(V==1)
			return p;
		//x = p + (1-p)y
		//y = rightOut(V-1)x
		return p / (1 - (1-p)*rightOut(p, V-1));
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const double& Expected, const double& Received) {
 bool ok = (abs(Expected - Received) < 1e-9);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, CatsOnTheCircle().getProb(N, K, p));}
int main(){

CASE(0)
	int N = 3; 
	int K = 1; 
	int p = 300000000; 
	double _ = 0.6999999999999985; 
END
CASE(1)
	int N = 6; 
	int K = 2; 
	int p = 500000000; 
	double _ = 0.2; 
END
CASE(2)
	int N = 6; 
	int K = 5; 
	int p = 500000000; 
	double _ = 0.2; 
END
CASE(3)
	int N = 10; 
	int K = 2; 
	int p = 666666666; 
	double _ = 0.00391389439551009; 
END
CASE(4)
	int N = 999999999; 
	int K = 999999996; 
	int p = 777777777; 
	double _ = 0.05830903870125612; 
END
CASE(5)
	int N = 1000000000; 
	int K = 4; 
	int p = 300000000; 
	double _ = 0.044981259448371; 
END
CASE(6)
	int N = 534428790; 
	int K = 459947197; 
	int p = 500000000; 
	double _ = 1.871156682766205E-9; 
END
/*
CASE(7)
	int N = ; 
	int K = ; 
	int p = ; 
	double _ = ; 
END
CASE(8)
	int N = ; 
	int K = ; 
	int p = ; 
	double _ = ; 
END
*/
}
// END CUT HERE