96fdfab500 2011-03-20 kinaba: #include <iostream> 96fdfab500 2011-03-20 kinaba: #include <sstream> 96fdfab500 2011-03-20 kinaba: #include <iomanip> 96fdfab500 2011-03-20 kinaba: #include <vector> 96fdfab500 2011-03-20 kinaba: #include <string> 96fdfab500 2011-03-20 kinaba: #include <map> 96fdfab500 2011-03-20 kinaba: #include <set> 96fdfab500 2011-03-20 kinaba: #include <algorithm> 96fdfab500 2011-03-20 kinaba: #include <numeric> 96fdfab500 2011-03-20 kinaba: #include <iterator> 96fdfab500 2011-03-20 kinaba: #include <functional> 96fdfab500 2011-03-20 kinaba: #include <complex> 96fdfab500 2011-03-20 kinaba: #include <queue> 96fdfab500 2011-03-20 kinaba: #include <stack> 96fdfab500 2011-03-20 kinaba: #include <cmath> 96fdfab500 2011-03-20 kinaba: #include <cassert> 96fdfab500 2011-03-20 kinaba: #include <cstring> 96fdfab500 2011-03-20 kinaba: using namespace std; 96fdfab500 2011-03-20 kinaba: typedef long long LL; 96fdfab500 2011-03-20 kinaba: typedef complex<double> CMP; 96fdfab500 2011-03-20 kinaba: 96fdfab500 2011-03-20 kinaba: class MafiaGame { public: 96fdfab500 2011-03-20 kinaba: double probabilityToLose(int N, vector <int> decisions) 96fdfab500 2011-03-20 kinaba: { 96fdfab500 2011-03-20 kinaba: vector<int> vc; 96fdfab500 2011-03-20 kinaba: { 96fdfab500 2011-03-20 kinaba: map<int,int> voteCnt; 96fdfab500 2011-03-20 kinaba: for(int i=0; i<decisions.size(); ++i) 96fdfab500 2011-03-20 kinaba: voteCnt[decisions[i]] ++; 96fdfab500 2011-03-20 kinaba: for(map<int,int>::iterator it=voteCnt.begin(); it!=voteCnt.end(); ++it) 96fdfab500 2011-03-20 kinaba: vc.push_back( it->second ); 96fdfab500 2011-03-20 kinaba: } 96fdfab500 2011-03-20 kinaba: const int MAX = *max_element(vc.begin(), vc.end()); 96fdfab500 2011-03-20 kinaba: if( MAX == 1 ) 96fdfab500 2011-03-20 kinaba: return 0.0; 96fdfab500 2011-03-20 kinaba: 96fdfab500 2011-03-20 kinaba: const int GotMAX = count(vc.begin(), vc.end(), MAX); 96fdfab500 2011-03-20 kinaba: for(int k=GotMAX; k; k=(N-k*MAX)%k) 96fdfab500 2011-03-20 kinaba: if( k == 1 ) 96fdfab500 2011-03-20 kinaba: return 1.0/GotMAX; 96fdfab500 2011-03-20 kinaba: return 0.0; 96fdfab500 2011-03-20 kinaba: } 96fdfab500 2011-03-20 kinaba: }; 96fdfab500 2011-03-20 kinaba: 96fdfab500 2011-03-20 kinaba: // BEGIN CUT HERE 96fdfab500 2011-03-20 kinaba: #include <ctime> 96fdfab500 2011-03-20 kinaba: double start_time; string timer() 96fdfab500 2011-03-20 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 96fdfab500 2011-03-20 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 96fdfab500 2011-03-20 kinaba: { os << "{ "; 96fdfab500 2011-03-20 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 96fdfab500 2011-03-20 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 96fdfab500 2011-03-20 kinaba: void verify_case(const double& Expected, const double& Received) { 96fdfab500 2011-03-20 kinaba: bool ok = (abs(Expected - Received) < 1e-9); 96fdfab500 2011-03-20 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 96fdfab500 2011-03-20 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 96fdfab500 2011-03-20 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 96fdfab500 2011-03-20 kinaba: #define END verify_case(_, MafiaGame().probabilityToLose(N, decisions));} 96fdfab500 2011-03-20 kinaba: int main(){ 96fdfab500 2011-03-20 kinaba: 96fdfab500 2011-03-20 kinaba: CASE(0) 96fdfab500 2011-03-20 kinaba: int N = 3; 96fdfab500 2011-03-20 kinaba: int decisions_[] = {1, 1, 1}; 96fdfab500 2011-03-20 kinaba: vector <int> decisions(decisions_, decisions_+sizeof(decisions_)/sizeof(*decisions_)); 96fdfab500 2011-03-20 kinaba: double _ = 1.0; 96fdfab500 2011-03-20 kinaba: END 96fdfab500 2011-03-20 kinaba: CASE(1) 96fdfab500 2011-03-20 kinaba: int N = 5; 96fdfab500 2011-03-20 kinaba: int decisions_[] = {1, 2, 3}; 96fdfab500 2011-03-20 kinaba: vector <int> decisions(decisions_, decisions_+sizeof(decisions_)/sizeof(*decisions_)); 96fdfab500 2011-03-20 kinaba: double _ = 0.0; 96fdfab500 2011-03-20 kinaba: END 96fdfab500 2011-03-20 kinaba: CASE(2) 96fdfab500 2011-03-20 kinaba: int N = 20; 96fdfab500 2011-03-20 kinaba: int decisions_[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 18, 19, 0}; 96fdfab500 2011-03-20 kinaba: vector <int> decisions(decisions_, decisions_+sizeof(decisions_)/sizeof(*decisions_)); 96fdfab500 2011-03-20 kinaba: double _ = 0.0; 96fdfab500 2011-03-20 kinaba: END 96fdfab500 2011-03-20 kinaba: CASE(3) 96fdfab500 2011-03-20 kinaba: int N = 23; 96fdfab500 2011-03-20 kinaba: int decisions_[] = {17, 10, 3, 14, 22, 5, 11, 10, 22, 3, 14, 5, 11, 17}; 96fdfab500 2011-03-20 kinaba: vector <int> decisions(decisions_, decisions_+sizeof(decisions_)/sizeof(*decisions_)); 96fdfab500 2011-03-20 kinaba: double _ = 0.14285714285714285; 96fdfab500 2011-03-20 kinaba: END 96fdfab500 2011-03-20 kinaba: /* 96fdfab500 2011-03-20 kinaba: CASE(4) 96fdfab500 2011-03-20 kinaba: int N = ; 96fdfab500 2011-03-20 kinaba: int decisions_[] = ; 96fdfab500 2011-03-20 kinaba: vector <int> decisions(decisions_, decisions_+sizeof(decisions_)/sizeof(*decisions_)); 96fdfab500 2011-03-20 kinaba: double _ = ; 96fdfab500 2011-03-20 kinaba: END 96fdfab500 2011-03-20 kinaba: CASE(5) 96fdfab500 2011-03-20 kinaba: int N = ; 96fdfab500 2011-03-20 kinaba: int decisions_[] = ; 96fdfab500 2011-03-20 kinaba: vector <int> decisions(decisions_, decisions_+sizeof(decisions_)/sizeof(*decisions_)); 96fdfab500 2011-03-20 kinaba: double _ = ; 96fdfab500 2011-03-20 kinaba: END 96fdfab500 2011-03-20 kinaba: */ 96fdfab500 2011-03-20 kinaba: } 96fdfab500 2011-03-20 kinaba: // END CUT HERE