#include <cstdio> #include <cstdlib> #include <algorithm> #include <cmath> #include <vector> #include <map> #include <set> #include <utility> using namespace std; typedef long long int int64; int64 get64(){ int inp; scanf("%d", &inp); return inp; } int64 m; void make(int64* lst, int64 x, int64 y, int64& r, int64& l, int64 st){ lst[st] = 1; int64 step = 1; for(;;){ st = (x*st + y)%m; step++; if(lst[st] != 0){ l = lst[st]-1; r = step-lst[st]; break; } lst[st] = step; } return; } int64 sp1[1000000] = {0}, sp2[1000000] = {0}; int64 inv(int64 a, int64 mm){ for(int64 lx = 1;lx <= mm-1;lx++) if((a*lx)%mm == 1) return lx; return 0; } int main(){ m = get64(); int64 h1 = get64(), a1 = get64(), x1 = get64(), y1 = get64(); int64 h2 = get64(), a2 = get64(), x2 = get64(), y2 = get64(); int64 l1, l2, r1, r2; make(sp1, x1, y1, r1, l1, h1); make(sp2, x2, y2, r2, l2, h2); if(sp1[a1] == 0 or sp2[a2] == 0){ puts("-1"); return 0; } int64 la1 = sp1[a1], la2 = sp2[a2]; int64 t1, t2; t1 = (la1 <= l1) ? 0:r1; t2 = (la2 <= l2) ? 0:r2; if(t1 == 0 and t2 == 0){ printf("%I64d\n", la1 == la2 ? (la1-1) : -1); return 0; } int64 gg = (t1 and t2) ? __gcd(t1, t2) : t1+t2; if((la1-la2)%gg != 0){ puts("-1"); return 0;} int64 dl = (la1-la2)/gg, d1 = t1/gg, d2 = t2/gg; int64 cal = d2 ? (-inv(d1, d2)*dl)%d2 : (-dl); if(d2 == 0 and cal < 0){ puts("-1"); return 0; } if(cal < 0) cal += d2; while(d1*cal+dl<0) cal += d2; printf("%I64d\n", t1*cal+la1-1); return 0; }
2015年5月27日 星期三
Codeforces Round #305 (Div. 1), problem: (A) Mike and Frog,
Case炸了QAQ
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言