Codeforces Round #305 (Div. 2)C. Mike and Frog 数学(循环节)
2015-05-27 20:35
351 查看
C. Mike and Frog
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
a modulo 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
Output
Input
Output
Note
In the first sample, heights sequences are following:
Xaniar:
Abol:
思路:首先找出每个的循环节,然后看a出现的位置是不是在循环节里面,如果不在,就只把a出现的位置加入到vector里面,否则把循环m次,每一次a出现的位置加到vector中,然后找一遍。
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
a modulo 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
5 4 2 1 1 0 1 2 3
Output
3
Input
10231 2
1 0
1 2
1 1
Output
-1
Note
In the first sample, heights sequences are following:
Xaniar:
Abol:
思路:首先找出每个的循环节,然后看a出现的位置是不是在循环节里面,如果不在,就只把a出现的位置加入到vector里面,否则把循环m次,每一次a出现的位置加到vector中,然后找一遍。
#include<iostream> #include<cstdio> #include<string> #include<cstring> #include<vector> #include<cmath> #include<queue> #include<stack> #include<map> #include<set> #include<algorithm> using namespace std; typedef long long LL; const int maxn=1000100; LL X[maxn],A[maxn]; LL H1,M,A1,X1,Y1,H2,A2,X2,Y2; LL st; int cal(LL *s,LL x,LL y,LL h) { LL id=0; while(1) { s[h]=id++; h=(h*x+y)%M; if(s[h]) { st=s[h]; break; } } return id-st; } vector<LL> solve(LL a,LL len,LL st) { vector<LL> tmp; if(a<st)tmp.push_back(a); else { for(int i=0;i<=M;i++) { LL x=a+i*len; tmp.push_back(x); } } return tmp; } int main() { cin>>M>>H1>>A1>>X1>>Y1>>H2>>A2>>X2>>Y2; LL Xlen=cal(X,X1,Y1,H1); int stX=st; LL Alen=cal(A,X2,Y2,H2); int stA=st; if(X[A1]==0||A[A2]==0) { printf("-1\n"); return 0; } vector<LL> t1=solve(X[A1],Xlen,stX); vector<LL> t2=solve(A[A2],Alen,stA); int l=0,r=0; LL ans=-1; while(l<t1.size()&&r<t2.size()) { if(t1[l]>t2[r])r++; else if(t1[l]<t2[r])l++; else {ans=t1[l];break;} } cout<<ans<<endl; return 0; }
相关文章推荐
- Codeforces Round #305 (Div. 1) A. Mike and Frog(数学)
- Codeforces Round #305 (Div. 2) 548C - Mike and Frog 循环节
- Codeforces 547 Div1 A. Mike and Frog(找循环节)
- Codeforces Round #305 (Div. 1)C. Mike and Foam 数学
- Codeforces 548C - Mike and Frog(暴力循环节)
- Codeforces Round #305 (Div. 2) C. Mike and Frog
- Codeforces Round #305 (Div. 1) A. Mike and Frog 暴力
- Codeforces Round #305 (Div. 2) C. Mike and Frog +B. Mike and Fun
- CF#305-C. Mike and Frog-数学模拟
- Codeforces Round #305 (Div. 2) C. Mike and Frog
- Codeforces Round #305 (Div. 2)C---Mike and Frog(扩欧+乱搞)
- CF #305 (Div. 2) C. Mike and Frog(扩展欧几里得&&当然暴力is also no problem)
- Codeforces Round #305 (Div. 2)——C拓展欧几里得,循环—— Mike and Frog
- 数论/暴力 Codeforces Round #305 (Div. 2) C. Mike and Frog
- Codeforces Round #305 (Div. 1)B - Mike and Feet 线段树
- set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet
- codeforces round 404 div2 D Anton and School - 2 组合数学
- Codeforces Round #199 (Div. 2) C. Cupboard and Balloons(数学)
- Codeforces Round #361 (Div. 2) 689A Mike and Cellphone
- Codeforces Round #259 (Div. 2) C - Little Pony and Expected Maximum (数学期望)