547A . Mike and Frog(codeforces Round 305)

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 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
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: 


题目大意:

     给出递推关系,起始值和目标值,求出最小的时刻两个同时到达目标值。

题解:

     由于m值组大为1000000,肯定会递推出循环节,且循环节不会超过m,于是可以用个数组存储下,a[x]存储的是递推到x的时刻。可以根据a数组求出循环节,并记录开始循环的位置。

    m1,m2分别为两个第一次出现目标值的时刻,nm1,nm2为不是循环节的最后的位置,于是分类讨论下。

    1.当m1<=nm1&&m2<=nm2(两个都不是循环节的部分)

        答案是m1

   2.当m1<=nm1||m2<=nm2(有一个不是循环节部分)

       判断是循环的部分是否可以在不是循环节到达的那个时刻到达。

  3.当m1>nm1&&m2>nm2(两个都是循环节部分)

     枚举一个循环节的个数,判断是否可以同时到达。


代码

   ps:我从时刻1开始算的,答案要减1

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1000000+100;
int a[maxn];
int b[maxn];
int main()
{
    int m,len1,len2,h1,h2,a1,a2,y1,y2,x1,x2;
    while(~scanf("%d",&m))
    {
        memset(a,-1,sizeof(a));
        memset(b,-1,sizeof(b));
        scanf("%d%d",&h1,&a1);
        scanf("%d%d",&x1,&y1);
        scanf("%d%d",&h2,&a2);
        scanf("%d%d",&x2,&y2);
        a[h1]=1;
        b[h2]=1;
        int nm1,nm2;
        int cur=1;
        while(1)
        {
            h1=((long long)x1*h1+y1)%m;
            if(a[h1]==-1)
                a[h1]=++cur;
            else
            {
                cur++;
                len1=cur-a[h1];
                nm1=cur-len1-1;
                break;
            }
        }
        cur=1;
        while(1)
        {
            h2=((long long)x2*h2+y2)%m;
            if(b[h2]==-1)
                b[h2]=++cur;
            else
            {
                cur++;
                len2=cur-b[h2];
                nm2=cur-len2-1;
                break;
            }
        }
        int m1=a[a1];
        int m2=b[a2];
        int sign=1;
        if(m1<=nm1&&m2<=nm2)
        {
            if(m1==m2&&m1>0)
            {
                sign=0;
                printf("%d\n",m1-1);
            }
        }
        else if(m1<=nm1||m2<=nm2)
        {
            if(m1<=nm1)
            {
                    if(m1>=m2&&(m1-m2)%len2==0)
                    {
                        sign=0;
                        long long ans=m1-1;
                        cout<<ans<<endl;
                        break;
                    }
            }
            if(m2<=nm2)
            {
                for(int i=0; i<=m; i++)
                {
                    if(m2>=m1&&(m2-m1)%len1==0)
                    {
                        sign=0;
                        long long ans=m2-1;
                        cout<<ans<<endl;
                        break;
                    }
                }
            }
        }

        else if(m1!=-1&&m2!=-1)
        {
            for(int i=0; i<=m; i++)
            {
                if((long long)i*len1+m1-m2>=0&&(((long long)i*len1+m1-m2)%len2==0))
                {
                    sign=0;
                    long long ans=(long long)i*len1+m1-1;
                    cout<<ans<<endl;
                    break;
                }
            }
        }
        if(sign)
            printf("-1\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值