#include #include #include #include using namespace std; #define debug if(0) typedef long long LL; const int N5 = 50015; int card[N5]; int perm[N5]; int inv[N5]; int n; bool color[N5]; int card_cycle[N5]; int card_pos_in_cycle[N5]; typedef vector >cykl; //pozycja, po ilu ruchach vectorcykle; void create_cycle(int v){ cykl res; int act = 0; while(1){ if(!color[v]) break; color[v] = false; int CARD = card[v]; card_pos_in_cycle[CARD] = act; card_cycle[CARD] = (int)cykle.size(); res.push_back(pair (v, act++)); //printf("v %d act %d\n",v,act); v = inv[v]; } debug { for(int i=0; i(pos, 0)) - CYKL.begin(); if(p==(int)CYKL.size() || CYKL[p].first!=pos) { return kiedy(-1,-1); } int fst = CYKL[p].second - card_pos_in_cycle[karta]; if(fst<0) fst+=CYKL.size(); return kiedy(fst, CYKL.size()); } int PROP[5]; LL A[5],B[5]; // x=A[i] mod B[i] /** CHINCZYK **/ LL GCD(LL a, LL b){ LL tmp; while(b!=0){ tmp = b; b = a % b; a = tmp; } return a; } LL eGCD(LL a, LL b, LL &x, LL &y){ if(b==0){ x = 1; y = 0; return a; } LL d = eGCD(b,a%b,y,x); y = a-x*(a/b)-y; x = b-x; return d; } LL inverse(LL a, LL p){LL x,y; eGCD(a%p+p, p, x, y); return x%p; } bool crt_error; LL CRT(LL a, LL m, LL b, LL n){ b = (b+n-(a%n))%n; LL d = GCD(m,n); if(b%d!=0){ crt_error = true; return 0; } LL old_m = m; m/=d; b/=d; n/=d; return ((b*inverse(m,n))%n)*old_m+a; } LL NWW(LL a, LL b){ LL res = a, d = GCD(a,b); res/=d; res*=b; return res; } LL chinczyk(){ crt_error = false; LL x; for(int i=1; i<5; i++){ x = CRT(A[0], B[0], A[i], B[i]); if(crt_error) return -1; A[0] = x; B[0] = NWW(B[0],B[i]); } return x; } /** CHINCZYK **/ void solve(){ int act = 0; for(int i=0; i=0){ debug printf("PLAYER %d : %lld\n", i+1, tmp); if(BEST==-1 || tmp