Codeforces Round #305 (Div. 2)C. Mike and Frog 数学(循环节)

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
5
4 2
1 1
0 1
2 3
Output
3
Input
1023
1 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;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值