Mike and Frog(CF547A),frogcf547a
A. Mike and Frogtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output
Mike has a frog and a flower. His frog is named Xaniar and his flower is named Abol. Initially(at time 0), height of Xaniar is h1 and height of Abol is h2. Each second, Mike waters Abol and Xaniar.
So, if height of Xaniar is h1 and height of Abol is h2, after one second height of Xaniar will become and height of Abol will become where x1, y1, x2 and y2 are some integer numbers and denotes the remainder of amodulo b.
Mike is a competitive programmer fan. He wants to know the minimum time it takes until height of Xania is a1 and height of Abol is a2.
Mike has asked you for your help. Calculate the minimum time or say it will never happen.
Input
The first line of input contains integer m (2 ≤ m ≤ 106).
The second line of input contains integers h1 and a1 (0 ≤ h1, a1 < m).
The third line of input contains integers x1 and y1 (0 ≤ x1, y1 < m).
The fourth line of input contains integers h2 and a2 (0 ≤ h2, a2 < m).
The fifth line of input contains integers x2 and y2 (0 ≤ x2, y2 < m).
It is guaranteed that h1 ≠ a1 and h2 ≠ a2.
Output
Print the minimum number of seconds until Xaniar reaches height a1 and Abol reaches height a2 or print -1 otherwise.
Sample test(s)input
54 21 10 12 3
output
3
input
10231 21 01 21 1
output
-1
Note
In the first sample, heights sequences are following:
Xaniar:
Abol:
題意:就是說h1=(h1*x1+y1)%m;h2=(h2*x2+y2)%m;經過t次變化,問你能否使得h1==a1;h2==a2;如果可以就輸出t;否則輸出-1
思路:考慮迴圈節;情況一:不管變化幾次都無法到達a1或a2;情況二:能到,但是a1或a2有一個不在,或者兩個都不在迴圈節內;情況三:在迴圈節內,利用同餘方程,擴充歐幾裡德求解;<此題有個大坑,那就是用歐幾裡德求解時候,x的值在運算過程中可能會溢出,要做特殊處理>
詳見代碼:
#include<stdio.h>#include<string.h>const int N = 1000006;#define LL __int64void exgcd(LL a,LL b,LL& d,LL& x,LL& y){ if(!b){d=a;x=1;y=0;} else { exgcd(b,a%b,d,y,x); y-=x*(a/b); }}bool vis1[N],vis2[N];int main(){ LL m,h1,h2,a1,a2,x1,x2,y1,y2; scanf("%I64d",&m); scanf("%I64d%I64d",&h1,&a1); scanf("%I64d%I64d",&x1,&y1); scanf("%I64d%I64d",&h2,&a2); scanf("%I64d%I64d",&x2,&y2); LL h=h1; memset(vis1,false,sizeof(vis1)); memset(vis2,false,sizeof(vis2)); bool bo=false; vis1[h1]=true; vis2[h2]=true; LL cnt1=0,tp1=0; LL cnt2=0,tp2=0; while(1) { h=(x1*h+y1)%m; if(!vis1[a1]) tp1++; if(vis1[h]) break; vis1[h]=true; } if(vis1[a1]) { memset(vis1,0,sizeof(vis1)); vis1[a1]=true; h=a1; cnt1=0; while(1) { h=(x1*h+y1)%m; cnt1++; if(vis1[h]) break; vis1[h]=true; } if(h!=a1) cnt1=0; h=h2; while(1) { h=(x2*h+y2)%m; cnt2++; if(!vis2[a2]) tp2++; if(vis2[h]) break; vis2[h]=true; } if(vis2[a2]) { memset(vis2,false,sizeof(vis2)); vis2[a2]=true; h=a2; cnt2=0; while(1) { h=(x2*h+y2)%m; cnt2++; if(vis2[h]) break; vis2[h]=true; } if(h!=a2) cnt2=0; if(cnt1==0||cnt2==0) { if(cnt1==0&&cnt2!=0) { LL tmp=tp1-tp2; if(tmp>=0&&tmp%cnt2==0) printf("%I64d\n",tp1); else printf("-1"); } else if(cnt2==0&&cnt1!=0) { LL tmp=tp2-tp1; if(tmp>=0&&tmp%cnt1==0) printf("%I64d\n",tp2); else printf("-1"); } else { if(tp1==tp2) printf("%I64d\n",tp1); else printf("-1\n"); } } else { if(tp1==tp2) printf("%I64d\n",tp1); else { LL a=cnt2; LL b=cnt1; LL c1=tp2; LL c2=tp1; LL c=c2-c1; LL d,x,y; exgcd(a,b,d,x,y); if(c%d) printf("-1\n"); else if(d==1&&x*y>0) { LL ax=1,by=1; while((ax*a%m+c1)!=(by*b%m+c2)) { if((ax*a%m+c1)-(by*b%m+c2)<0) ax++; else by++; } printf("%I64d\n",ax*a+c1); } else { x=x*(c/d); LL mm=b/d; x=(x%mm+mm)%mm; printf("%I64d\n",c1+a*x); } } } } else printf("-1\n"); } else printf("-1\n"); return 0;}
大神代碼:
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;#define out(x) {printf("%I64d\n",(long long)(x));return 0;}int m,h1,a1,x1,y1,h2,a2,x2,y2,p,q,c,dx,dy;int main(){ scanf("%d",&m); scanf("%d%d%d%d",&h1,&a1,&x1,&y1); scanf("%d%d%d%d",&h2,&a2,&x2,&y2); for(p=1;p<=m;p++){ h1=(1LL*h1*x1+y1)%m; h2=(1LL*h2*x2+y2)%m; if(h1==a1)break; } if(p>m)out(-1); if(h1==a1&&h2==a2)out(p); for(c=1;c<=m;c++){ h1=(1LL*h1*x1+y1)%m; if(h1==a1)break; } if(c>m)out(-1); dx=1;dy=0; for(int i(1);i<=c;i++){ dx=(1LL*dx*x2)%m; dy=(1LL*dy*x2+y2)%m; } for(int i(1);i<=m;i++){ h2=(1LL*dx*h2+dy)%m; if(h2==a2)out(p+1LL*c*i); } out(-1); return 0;}