【CodeForces】A. Mike and Frog

A. Mike and Frog
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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 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




快被这题坑死了,大题思路:

找到第一次到达a1的步数记录为p1,找到从a1再一次到达a1的步数q1

同理记录p2、q2

之后找到一个最小的数 t = p1 + q1 * x = p2 + q2 * y

这里的t肯定是小于 max(p1,p2) + q1 * q2的

#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn = 1000005;
bool vis[maxn];
LL mod;
LL x1,y1,h1,a1,p1,q1;
LL x2,y2,h2,a2,p2,q2;
void solve(LL h,LL a,LL x,LL y,LL &p,LL &q){
    memset(vis,0,sizeof(vis));
    LL now = h,cnt = 0;
    p = -1,q = -1;
    while(1){
        if(vis[now]) break;
        if(now == a) {p = cnt; break;}
        vis[now] = 1;
        now = (now * x + y) % mod;
        cnt ++;
    }
    now = a; cnt = 0;
    memset(vis,0,sizeof(vis));
    while(1){
        if(vis[now]){
            if(now == a) q = cnt;
            break;
        }
        vis[now] = 1;
        now = (x * now + y) % mod;
        cnt ++;
    }
}
int main(){
    cin >> mod;
    cin >> h1 >> a1 >> x1 >> y1;
    cin >> h2 >> a2 >> x2 >> y2;
    solve(h1,a1,x1,y1,p1,q1);
    solve(h2,a2,x2,y2,p2,q2);
    if(p1 == -1 || p2 == -1)
        cout << "-1" << endl;
    else if(q1 == -1 && q2 == -1){
        if(p1 == p2)
            cout << p1 << endl;
        else
            cout << "-1" << endl;
    }
    else if(q1 == -1){
        if(p1 >= p2){
            LL e = p1 - p2;
            if(e % q2 == 0)
                cout << p1 << endl;
            else
                cout << "-1" << endl;
        }
        else
            cout << "-1" << endl;
    }
    else if(q2 == -1){
        if(p2 >= p1){
            LL e = p2 - p1;
            if(e % q1 == 0)
                cout << p2 << endl;
            else
                cout << "-1" << endl;
        }
        else
            cout << "-1" << endl;
    }
    else{
        LL max_v = max(p1,p2) + q2 * q1;
        LL ok = -1;
        for(LL i = p1; i <= max_v; i += q1){
            LL d = i - p2;
            if(d >= 0 && (d % q2 == 0)){
                ok = i;
                break;
            }
        }
        cout << ok << endl;
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值