5b7cb6883d 2015-09-28 kinaba: #include <iostream> 5b7cb6883d 2015-09-28 kinaba: #include <sstream> 5b7cb6883d 2015-09-28 kinaba: #include <iomanip> 5b7cb6883d 2015-09-28 kinaba: #include <vector> 5b7cb6883d 2015-09-28 kinaba: #include <string> 5b7cb6883d 2015-09-28 kinaba: #include <map> 5b7cb6883d 2015-09-28 kinaba: #include <set> 5b7cb6883d 2015-09-28 kinaba: #include <algorithm> 5b7cb6883d 2015-09-28 kinaba: #include <numeric> 5b7cb6883d 2015-09-28 kinaba: #include <iterator> 5b7cb6883d 2015-09-28 kinaba: #include <functional> 5b7cb6883d 2015-09-28 kinaba: #include <complex> 5b7cb6883d 2015-09-28 kinaba: #include <queue> 5b7cb6883d 2015-09-28 kinaba: #include <stack> 5b7cb6883d 2015-09-28 kinaba: #include <cmath> 5b7cb6883d 2015-09-28 kinaba: #include <cassert> 5b7cb6883d 2015-09-28 kinaba: #include <tuple> 5b7cb6883d 2015-09-28 kinaba: using namespace std; 5b7cb6883d 2015-09-28 kinaba: typedef long long LL; 5b7cb6883d 2015-09-28 kinaba: typedef complex<double> CMP; 5b7cb6883d 2015-09-28 kinaba: 5b7cb6883d 2015-09-28 kinaba: class CatsOnTheCircle { public: 5b7cb6883d 2015-09-28 kinaba: int N; 5b7cb6883d 2015-09-28 kinaba: double p; 5b7cb6883d 2015-09-28 kinaba: 5b7cb6883d 2015-09-28 kinaba: double getProb(int N_, int K, int p_) 5b7cb6883d 2015-09-28 kinaba: { 5b7cb6883d 2015-09-28 kinaba: N = N_; 5b7cb6883d 2015-09-28 kinaba: p = p_ / pow(10.0,9); 5b7cb6883d 2015-09-28 kinaba: return rec(false, 1, K-1); 5b7cb6883d 2015-09-28 kinaba: } 5b7cb6883d 2015-09-28 kinaba: /* 5b7cb6883d 2015-09-28 kinaba: void rec2(int V, 5b7cb6883d 2015-09-28 kinaba: vector<double>* p_t2e, 5b7cb6883d 2015-09-28 kinaba: vector<double>* q_t2e) { 5b7cb6883d 2015-09-28 kinaba: if(V == N-1) { 5b7cb6883d 2015-09-28 kinaba: p_t2e->assign(1, 1.0); 5b7cb6883d 2015-09-28 kinaba: q_t2e->assign(1, 1.0); 5b7cb6883d 2015-09-28 kinaba: return; 5b7cb6883d 2015-09-28 kinaba: } 5b7cb6883d 2015-09-28 kinaba: 5b7cb6883d 2015-09-28 kinaba: vector<double> pp, qq; 5b7cb6883d 2015-09-28 kinaba: rec2(V+1, &pp, &qq); 5b7cb6883d 2015-09-28 kinaba: 5b7cb6883d 2015-09-28 kinaba: vector<double> pp2; 5b7cb6883d 2015-09-28 kinaba: pp2 += (pp<<1) * rightOut(p,V); 5b7cb6883d 2015-09-28 kinaba: pp2 += rev(qq<<1) * (1-rightOut(p,V)); 5b7cb6883d 2015-09-28 kinaba: } 5b7cb6883d 2015-09-28 kinaba: */ 5b7cb6883d 2015-09-28 kinaba: double rec(bool inv, int V, int T) { 5b7cb6883d 2015-09-28 kinaba: if(V == N-1) 5b7cb6883d 2015-09-28 kinaba: return 1.0; 5b7cb6883d 2015-09-28 kinaba: double pp = 0.0; 5b7cb6883d 2015-09-28 kinaba: if(T!=0) pp += rightOut(inv?1-p:p,V) * rec(inv, V+1,T-1); 5b7cb6883d 2015-09-28 kinaba: if(T!=N-V-1) pp += (1-rightOut(inv?1-p:p,V)) * rec(!inv, V+1,N-V-2-T); 5b7cb6883d 2015-09-28 kinaba: return pp; 5b7cb6883d 2015-09-28 kinaba: } 5b7cb6883d 2015-09-28 kinaba: 5b7cb6883d 2015-09-28 kinaba: double rightOut(const double p, int V) { 5b7cb6883d 2015-09-28 kinaba: if(V==1) 5b7cb6883d 2015-09-28 kinaba: return p; 5b7cb6883d 2015-09-28 kinaba: //x = p + (1-p)y 5b7cb6883d 2015-09-28 kinaba: //y = rightOut(V-1)x 5b7cb6883d 2015-09-28 kinaba: return p / (1 - (1-p)*rightOut(p, V-1)); 5b7cb6883d 2015-09-28 kinaba: } 5b7cb6883d 2015-09-28 kinaba: }; 5b7cb6883d 2015-09-28 kinaba: 5b7cb6883d 2015-09-28 kinaba: // BEGIN CUT HERE 5b7cb6883d 2015-09-28 kinaba: #include <ctime> 5b7cb6883d 2015-09-28 kinaba: double start_time; string timer() 5b7cb6883d 2015-09-28 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 5b7cb6883d 2015-09-28 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 5b7cb6883d 2015-09-28 kinaba: { os << "{ "; 5b7cb6883d 2015-09-28 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 5b7cb6883d 2015-09-28 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 5b7cb6883d 2015-09-28 kinaba: void verify_case(const double& Expected, const double& Received) { 5b7cb6883d 2015-09-28 kinaba: bool ok = (abs(Expected - Received) < 1e-9); 5b7cb6883d 2015-09-28 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 5b7cb6883d 2015-09-28 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 5b7cb6883d 2015-09-28 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 5b7cb6883d 2015-09-28 kinaba: #define END verify_case(_, CatsOnTheCircle().getProb(N, K, p));} 5b7cb6883d 2015-09-28 kinaba: int main(){ 5b7cb6883d 2015-09-28 kinaba: 5b7cb6883d 2015-09-28 kinaba: CASE(0) 5b7cb6883d 2015-09-28 kinaba: int N = 3; 5b7cb6883d 2015-09-28 kinaba: int K = 1; 5b7cb6883d 2015-09-28 kinaba: int p = 300000000; 5b7cb6883d 2015-09-28 kinaba: double _ = 0.6999999999999985; 5b7cb6883d 2015-09-28 kinaba: END 5b7cb6883d 2015-09-28 kinaba: CASE(1) 5b7cb6883d 2015-09-28 kinaba: int N = 6; 5b7cb6883d 2015-09-28 kinaba: int K = 2; 5b7cb6883d 2015-09-28 kinaba: int p = 500000000; 5b7cb6883d 2015-09-28 kinaba: double _ = 0.2; 5b7cb6883d 2015-09-28 kinaba: END 5b7cb6883d 2015-09-28 kinaba: CASE(2) 5b7cb6883d 2015-09-28 kinaba: int N = 6; 5b7cb6883d 2015-09-28 kinaba: int K = 5; 5b7cb6883d 2015-09-28 kinaba: int p = 500000000; 5b7cb6883d 2015-09-28 kinaba: double _ = 0.2; 5b7cb6883d 2015-09-28 kinaba: END 5b7cb6883d 2015-09-28 kinaba: CASE(3) 5b7cb6883d 2015-09-28 kinaba: int N = 10; 5b7cb6883d 2015-09-28 kinaba: int K = 2; 5b7cb6883d 2015-09-28 kinaba: int p = 666666666; 5b7cb6883d 2015-09-28 kinaba: double _ = 0.00391389439551009; 5b7cb6883d 2015-09-28 kinaba: END 5b7cb6883d 2015-09-28 kinaba: CASE(4) 5b7cb6883d 2015-09-28 kinaba: int N = 999999999; 5b7cb6883d 2015-09-28 kinaba: int K = 999999996; 5b7cb6883d 2015-09-28 kinaba: int p = 777777777; 5b7cb6883d 2015-09-28 kinaba: double _ = 0.05830903870125612; 5b7cb6883d 2015-09-28 kinaba: END 5b7cb6883d 2015-09-28 kinaba: CASE(5) 5b7cb6883d 2015-09-28 kinaba: int N = 1000000000; 5b7cb6883d 2015-09-28 kinaba: int K = 4; 5b7cb6883d 2015-09-28 kinaba: int p = 300000000; 5b7cb6883d 2015-09-28 kinaba: double _ = 0.044981259448371; 5b7cb6883d 2015-09-28 kinaba: END 5b7cb6883d 2015-09-28 kinaba: CASE(6) 5b7cb6883d 2015-09-28 kinaba: int N = 534428790; 5b7cb6883d 2015-09-28 kinaba: int K = 459947197; 5b7cb6883d 2015-09-28 kinaba: int p = 500000000; 5b7cb6883d 2015-09-28 kinaba: double _ = 1.871156682766205E-9; 5b7cb6883d 2015-09-28 kinaba: END 5b7cb6883d 2015-09-28 kinaba: /* 5b7cb6883d 2015-09-28 kinaba: CASE(7) 5b7cb6883d 2015-09-28 kinaba: int N = ; 5b7cb6883d 2015-09-28 kinaba: int K = ; 5b7cb6883d 2015-09-28 kinaba: int p = ; 5b7cb6883d 2015-09-28 kinaba: double _ = ; 5b7cb6883d 2015-09-28 kinaba: END 5b7cb6883d 2015-09-28 kinaba: CASE(8) 5b7cb6883d 2015-09-28 kinaba: int N = ; 5b7cb6883d 2015-09-28 kinaba: int K = ; 5b7cb6883d 2015-09-28 kinaba: int p = ; 5b7cb6883d 2015-09-28 kinaba: double _ = ; 5b7cb6883d 2015-09-28 kinaba: END 5b7cb6883d 2015-09-28 kinaba: */ 5b7cb6883d 2015-09-28 kinaba: } 5b7cb6883d 2015-09-28 kinaba: // END CUT HERE