7764536bd3 2013-11-29 kinaba: #include <iostream> 7764536bd3 2013-11-29 kinaba: #include <sstream> 7764536bd3 2013-11-29 kinaba: #include <iomanip> 7764536bd3 2013-11-29 kinaba: #include <vector> 7764536bd3 2013-11-29 kinaba: #include <string> 7764536bd3 2013-11-29 kinaba: #include <map> 7764536bd3 2013-11-29 kinaba: #include <set> 7764536bd3 2013-11-29 kinaba: #include <algorithm> 7764536bd3 2013-11-29 kinaba: #include <numeric> 7764536bd3 2013-11-29 kinaba: #include <iterator> 7764536bd3 2013-11-29 kinaba: #include <functional> 7764536bd3 2013-11-29 kinaba: #include <complex> 7764536bd3 2013-11-29 kinaba: #include <queue> 7764536bd3 2013-11-29 kinaba: #include <stack> 7764536bd3 2013-11-29 kinaba: #include <cmath> 7764536bd3 2013-11-29 kinaba: #include <cassert> 7764536bd3 2013-11-29 kinaba: #include <tuple> 7764536bd3 2013-11-29 kinaba: using namespace std; 7764536bd3 2013-11-29 kinaba: typedef long long LL; 7764536bd3 2013-11-29 kinaba: typedef complex<double> CMP; 7764536bd3 2013-11-29 kinaba: 7764536bd3 2013-11-29 kinaba: LL gcd(LL a, LL b) 7764536bd3 2013-11-29 kinaba: { 7764536bd3 2013-11-29 kinaba: while(a) 7764536bd3 2013-11-29 kinaba: swap(a, b%=a); 7764536bd3 2013-11-29 kinaba: return b; 7764536bd3 2013-11-29 kinaba: } 7764536bd3 2013-11-29 kinaba: 7764536bd3 2013-11-29 kinaba: class ConvexPolygonGame { public: 7764536bd3 2013-11-29 kinaba: string winner(vector <int> X, vector <int> Y) 7764536bd3 2013-11-29 kinaba: { 7764536bd3 2013-11-29 kinaba: return has_int_triangle(X, Y) ? "Masha" : "Petya"; 7764536bd3 2013-11-29 kinaba: } 7764536bd3 2013-11-29 kinaba: 7764536bd3 2013-11-29 kinaba: bool has_int_triangle(const vector<int>& X, const vector<int>& Y) 7764536bd3 2013-11-29 kinaba: { 7764536bd3 2013-11-29 kinaba: const int xm = *min_element(X.begin(), X.end()); 7764536bd3 2013-11-29 kinaba: const int xM = *max_element(X.begin(), X.end()); 7764536bd3 2013-11-29 kinaba: 7764536bd3 2013-11-29 kinaba: vector< pair<int,int> > p; 7764536bd3 2013-11-29 kinaba: 7764536bd3 2013-11-29 kinaba: int nc = 0, two = 0, dis = 0; 7764536bd3 2013-11-29 kinaba: for(int x=xm; x<=xM; ++x) 7764536bd3 2013-11-29 kinaba: { 7764536bd3 2013-11-29 kinaba: int ym, yM; 7764536bd3 2013-11-29 kinaba: tie(ym, yM) = y_range(x, X, Y); 7764536bd3 2013-11-29 kinaba: if(ym <= yM) { 7764536bd3 2013-11-29 kinaba: dis ++; 7764536bd3 2013-11-29 kinaba: nc += (yM - ym + 1); 7764536bd3 2013-11-29 kinaba: if(yM-ym+1 >= 2) 7764536bd3 2013-11-29 kinaba: two ++; 7764536bd3 2013-11-29 kinaba: else 7764536bd3 2013-11-29 kinaba: p.emplace_back(x, ym); 7764536bd3 2013-11-29 kinaba: } 7764536bd3 2013-11-29 kinaba: // cerr << nc << "," << two << " : " << x << " " << ym << " ~ " << yM << endl; 7764536bd3 2013-11-29 kinaba: if(nc>=3 && two>=1 && dis>=2) 7764536bd3 2013-11-29 kinaba: return true; 7764536bd3 2013-11-29 kinaba: } 7764536bd3 2013-11-29 kinaba: if(nc>=3 && two==0 && dis>=2) 7764536bd3 2013-11-29 kinaba: { 7764536bd3 2013-11-29 kinaba: double am, aM; 7764536bd3 2013-11-29 kinaba: for(int i=1; i<p.size(); ++i) 7764536bd3 2013-11-29 kinaba: { 7764536bd3 2013-11-29 kinaba: double a = double(p[i].second - p[0].second) / (p[i].first - p[0].first); 7764536bd3 2013-11-29 kinaba: if(i==1)am=aM=a; 7764536bd3 2013-11-29 kinaba: else am=min(am, a), aM=max(aM,a); 7764536bd3 2013-11-29 kinaba: } 7764536bd3 2013-11-29 kinaba: return (aM-am) > 1e-9; 7764536bd3 2013-11-29 kinaba: } 7764536bd3 2013-11-29 kinaba: return false; 7764536bd3 2013-11-29 kinaba: } 7764536bd3 2013-11-29 kinaba: 7764536bd3 2013-11-29 kinaba: tuple<int,int> y_range(int x, const vector<int>& X, const vector<int>& Y) 7764536bd3 2013-11-29 kinaba: { 7764536bd3 2013-11-29 kinaba: vector< pair<LL,LL> > cross; 7764536bd3 2013-11-29 kinaba: vector< pair<LL,LL> > exact; 7764536bd3 2013-11-29 kinaba: 7764536bd3 2013-11-29 kinaba: const int N = X.size(); 7764536bd3 2013-11-29 kinaba: for(int i=0; i<N; ++i) 7764536bd3 2013-11-29 kinaba: { 7764536bd3 2013-11-29 kinaba: int x1=X[i], x2=X[(i+1)%N]; 7764536bd3 2013-11-29 kinaba: int y1=Y[i], y2=Y[(i+1)%N]; 7764536bd3 2013-11-29 kinaba: if(x1 > x2) 7764536bd3 2013-11-29 kinaba: swap(x1, x2), swap(y1, y2); 7764536bd3 2013-11-29 kinaba: 7764536bd3 2013-11-29 kinaba: if(x1 == x2) 7764536bd3 2013-11-29 kinaba: { 7764536bd3 2013-11-29 kinaba: if(x == x1) { 7764536bd3 2013-11-29 kinaba: cross.emplace_back(y1,1); 7764536bd3 2013-11-29 kinaba: cross.emplace_back(y2,1); 7764536bd3 2013-11-29 kinaba: exact.emplace_back(y1,1); 7764536bd3 2013-11-29 kinaba: exact.emplace_back(y2,1); 7764536bd3 2013-11-29 kinaba: } 7764536bd3 2013-11-29 kinaba: } 7764536bd3 2013-11-29 kinaba: else 7764536bd3 2013-11-29 kinaba: { 7764536bd3 2013-11-29 kinaba: if(x<x1 || x2<x) 7764536bd3 2013-11-29 kinaba: continue; 7764536bd3 2013-11-29 kinaba: LL q = LL(y2-y1)*(x-x1) + LL(y1)*(x2-x1); 7764536bd3 2013-11-29 kinaba: LL d = LL(x2-x1); 7764536bd3 2013-11-29 kinaba: LL g = gcd(abs(q), d); 7764536bd3 2013-11-29 kinaba: cross.emplace_back(q/g, d/g); 7764536bd3 2013-11-29 kinaba: if(x==x1 || x==x2) 7764536bd3 2013-11-29 kinaba: exact.emplace_back(q/g, d/g); 7764536bd3 2013-11-29 kinaba: } 7764536bd3 2013-11-29 kinaba: } 7764536bd3 2013-11-29 kinaba: 7764536bd3 2013-11-29 kinaba: sort(cross.begin(), cross.end()); 7764536bd3 2013-11-29 kinaba: cross.erase(unique(cross.begin(), cross.end()), cross.end()); 7764536bd3 2013-11-29 kinaba: if(cross.size() == 2) { 7764536bd3 2013-11-29 kinaba: LL q1 = cross[0].first; 7764536bd3 2013-11-29 kinaba: LL d1 = cross[0].second; 7764536bd3 2013-11-29 kinaba: LL q2 = cross[1].first; 7764536bd3 2013-11-29 kinaba: LL d2 = cross[1].second; 7764536bd3 2013-11-29 kinaba: if(q1*d2 > q2*d1) 7764536bd3 2013-11-29 kinaba: swap(q1,q2), swap(d1,d2); 7764536bd3 2013-11-29 kinaba: int ym = ceil_div(q1, d1); 7764536bd3 2013-11-29 kinaba: int yM = floor_div(q2, d2); 7764536bd3 2013-11-29 kinaba: bool ym_exact = false, yM_exact = false; 7764536bd3 2013-11-29 kinaba: for(int i=0; i<exact.size(); ++i) { 7764536bd3 2013-11-29 kinaba: if(ym*exact[i].second == exact[i].first) 7764536bd3 2013-11-29 kinaba: ym_exact = true; 7764536bd3 2013-11-29 kinaba: if(yM*exact[i].second == exact[i].first) 7764536bd3 2013-11-29 kinaba: yM_exact = true; 7764536bd3 2013-11-29 kinaba: } 7764536bd3 2013-11-29 kinaba: return make_tuple(ym_exact ? ym+1 : ym, yM_exact ? yM-1 : yM); 7764536bd3 2013-11-29 kinaba: } 7764536bd3 2013-11-29 kinaba: return make_tuple(-9998, -9999); 7764536bd3 2013-11-29 kinaba: } 7764536bd3 2013-11-29 kinaba: 7764536bd3 2013-11-29 kinaba: static int floor_div(LL a, LL b) 7764536bd3 2013-11-29 kinaba: { 7764536bd3 2013-11-29 kinaba: if(a>=0) return int(a/b); 7764536bd3 2013-11-29 kinaba: return int(-((-a+b-1)/b)); 7764536bd3 2013-11-29 kinaba: } 7764536bd3 2013-11-29 kinaba: 7764536bd3 2013-11-29 kinaba: static int ceil_div(LL a, LL b) 7764536bd3 2013-11-29 kinaba: { 7764536bd3 2013-11-29 kinaba: if(a>=0) return int((a+b-1)/b); 7764536bd3 2013-11-29 kinaba: return int(-((-a)/b)); 7764536bd3 2013-11-29 kinaba: } 7764536bd3 2013-11-29 kinaba: }; 7764536bd3 2013-11-29 kinaba: 7764536bd3 2013-11-29 kinaba: // BEGIN CUT HERE 7764536bd3 2013-11-29 kinaba: #include <ctime> 7764536bd3 2013-11-29 kinaba: double start_time; string timer() 7764536bd3 2013-11-29 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 7764536bd3 2013-11-29 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 7764536bd3 2013-11-29 kinaba: { os << "{ "; 7764536bd3 2013-11-29 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 7764536bd3 2013-11-29 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 7764536bd3 2013-11-29 kinaba: void verify_case(const string& Expected, const string& Received) { 7764536bd3 2013-11-29 kinaba: bool ok = (Expected == Received); 7764536bd3 2013-11-29 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 7764536bd3 2013-11-29 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 7764536bd3 2013-11-29 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 7764536bd3 2013-11-29 kinaba: #define END verify_case(_, ConvexPolygonGame().winner(X, Y));} 7764536bd3 2013-11-29 kinaba: int main(){ 7764536bd3 2013-11-29 kinaba: 7764536bd3 2013-11-29 kinaba: CASE(0) 7764536bd3 2013-11-29 kinaba: int X_[] = {0, 1, 0}; 7764536bd3 2013-11-29 kinaba: vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_)); 7764536bd3 2013-11-29 kinaba: int Y_[] = {0, 0, 1}; 7764536bd3 2013-11-29 kinaba: vector <int> Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); 7764536bd3 2013-11-29 kinaba: string _ = "Petya"; 7764536bd3 2013-11-29 kinaba: END 7764536bd3 2013-11-29 kinaba: CASE(1) 7764536bd3 2013-11-29 kinaba: int X_[] = {0, 4, 2}; 7764536bd3 2013-11-29 kinaba: vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_)); 7764536bd3 2013-11-29 kinaba: int Y_[] = {0, 0, 2}; 7764536bd3 2013-11-29 kinaba: vector <int> Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); 7764536bd3 2013-11-29 kinaba: string _ = "Masha"; 7764536bd3 2013-11-29 kinaba: END 7764536bd3 2013-11-29 kinaba: CASE(2) 7764536bd3 2013-11-29 kinaba: int X_[] = {0, 100, 100, 0}; 7764536bd3 2013-11-29 kinaba: vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_)); 7764536bd3 2013-11-29 kinaba: int Y_[] = {0, 0, 100, 100}; 7764536bd3 2013-11-29 kinaba: vector <int> Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); 7764536bd3 2013-11-29 kinaba: string _ = "Masha"; 7764536bd3 2013-11-29 kinaba: END 7764536bd3 2013-11-29 kinaba: CASE(3) 7764536bd3 2013-11-29 kinaba: int X_[] = {0, 50, 100, 50}; 7764536bd3 2013-11-29 kinaba: vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_)); 7764536bd3 2013-11-29 kinaba: int Y_[] = {0, -1, 0, 1}; 7764536bd3 2013-11-29 kinaba: vector <int> Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); 7764536bd3 2013-11-29 kinaba: string _ = "Petya"; 7764536bd3 2013-11-29 kinaba: END 7764536bd3 2013-11-29 kinaba: CASE(4) 7764536bd3 2013-11-29 kinaba: int X_[] = {-100000, 100000, 100000, -100000}; 7764536bd3 2013-11-29 kinaba: vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_)); 7764536bd3 2013-11-29 kinaba: int Y_[] = {-1, -1, 1, 1}; 7764536bd3 2013-11-29 kinaba: vector <int> Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); 7764536bd3 2013-11-29 kinaba: string _ = "Masha"; 7764536bd3 2013-11-29 kinaba: END 7764536bd3 2013-11-29 kinaba: CASE(5) 7764536bd3 2013-11-29 kinaba: // 1 1 1 (yoko) 7764536bd3 2013-11-29 kinaba: int X_[] = {0, 3, 5, 3}; 7764536bd3 2013-11-29 kinaba: vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_)); 7764536bd3 2013-11-29 kinaba: int Y_[] = {0, 0, 1, 1}; 7764536bd3 2013-11-29 kinaba: vector <int> Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); 7764536bd3 2013-11-29 kinaba: string _ = "Masha"; 7764536bd3 2013-11-29 kinaba: END 7764536bd3 2013-11-29 kinaba: CASE(5) 7764536bd3 2013-11-29 kinaba: // 1 1 1 (tate) 7764536bd3 2013-11-29 kinaba: int X_[] = {0, 1, 1, 0}; 7764536bd3 2013-11-29 kinaba: vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_)); 7764536bd3 2013-11-29 kinaba: int Y_[] = {0, 3, 5, 3}; 7764536bd3 2013-11-29 kinaba: vector <int> Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); 7764536bd3 2013-11-29 kinaba: string _ = "Masha"; 7764536bd3 2013-11-29 kinaba: END 7764536bd3 2013-11-29 kinaba: CASE(6) 7764536bd3 2013-11-29 kinaba: // 3 (tate) // yoko ha sample 7764536bd3 2013-11-29 kinaba: int X_[] = {-1, 0, 1, 0}; 7764536bd3 2013-11-29 kinaba: vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_)); 7764536bd3 2013-11-29 kinaba: int Y_[] = {0, -2, 0, 2}; 7764536bd3 2013-11-29 kinaba: vector <int> Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); 7764536bd3 2013-11-29 kinaba: string _ = "Petya"; 7764536bd3 2013-11-29 kinaba: END 7764536bd3 2013-11-29 kinaba: } 7764536bd3 2013-11-29 kinaba: // END CUT HERE