Mike and Frog (CF547A), frogcf547a

Source: Internet
Author: User
Tags time 0

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 isH1 and height of Abol isH2. Each second, Mike waters Abol and Xaniar.

So, if height of Xaniar isH1 and height of Abol isH2, after one second height of Xaniar will become and height of Abol will become whereX1, bytes,Y1, bytes,X2 andY2 are some integer numbers and denotes the remainderAModuloB.

Mike is a competitive programmer fan. He wants to know the minimum time it takes until height of Xania isA1 and height of Abol isA2.

Mike has asked you for your help. Calculate the minimum time or say it will never happen.

Input

The first line of input contains integerM(2 cores ≤ CoresMLimit ≤ limit 106 ).

The second line of input contains integersH1 andA1 (0 bytes ≤ bytesH1, bytes,A1 worker <workerM).

The third line of input contains integersX1 andY1 (0 bytes ≤ bytesX1, bytes,Y1 worker <workerM).

The fourth line of input contains integersH2 andA2 (0 bytes ≤ bytesH2, bytes,A2 bytes <averageM).

The maximum th line of input contains integersX2 andY2 (0 bytes ≤ bytesX2, bytes,Y2 bytes <averageM).

It is guaranteed thatH1 bytes = bytesA1 andH2 bytes = bytesA2.

Output

Print the minimum number of seconds until Xaniar reaches heightA1 and Abol reaches heightA2 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:

 

Meaning: h1 = (h1 * x1 + y1) % m; h2 = (h2 * x2 + y2) % m; after t changes, ask if you can make h1 = a1; h2 = a2; if you can, output t; otherwise output-1

Idea: Consider loop section. Case 1: a1 or a2 cannot be reached no matter how many changes occur. Case 2: Yes, but either a1 or a2 is absent, or neither of them is in the circular section. Case 3: In the circular section, we use the same remainder equation to extend the Euclidean solution. <this question has a big pitfall, that is, when we use Euclidean to solve the problem, the value of x may overflow during operation. Special processing is required.>

For details, see the code:

#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;}


 

 

 

Great God code:

 

#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;}


 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.