Codeforces Round #305 (Div. 2) C 循环节

来源:互联网 发布:比特币原理与挖矿算法 编辑:程序博客网 时间:2024/06/05 17:53



链接:戳这里


C. Mike and Frog
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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.

Examples
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,h1,a1,x1,y1,h2,a2,x2,y2。每单位时间做的操作如下
    h1 = ((h1 * x1) + y1) % m
    h2 = ((h2 * x2) + y2) % m

问是否有一个时间使得h1==a1 && h2==a2,没有输出-1


思路:

找出第一个h1==a1的时间p1,以及一个循环之后h1==a1的时间q1

找出第一个h2==a2的时间p2,以及一个循环之后h2==a2的时间q2

然后暴力判是否存在一个时间使得p1、p2分别经过x、y次循环之后达到同一个值


代码:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include <ctime>#include<queue>#include<set>#include<map>#include<stack>#include<iomanip>#include<cmath>#define mst(ss,b) memset((ss),(b),sizeof(ss))#define maxn 0x3f3f3f3f#define MAX 1000100///#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;typedef unsigned long long ull;#define INF (1ll<<60)-1using namespace std;ll m,h1,a1,h2,a2,x1,y1,x2,y2;int main(){    scanf("%I64d",&m);    scanf("%I64d%I64d",&h1,&a1);    scanf("%I64d%I64d",&x1,&y1);    scanf("%I64d%I64d",&h2,&a2);    scanf("%I64d%I64d",&x2,&y2);    ll p1=-1,p2=-1,q1=0,q2=0;    for(int i=1;i<=m;i++){        h1=(x1*h1%m+y1)%m;        if(h1==a1){            p1=i;            break;        }    }    for(int i=1;i<=m;i++){        h1=(x1*h1%m+y1)%m;        if(h1==a1){            q1=i;            break;        }    }    for(int i=1;i<=m;i++){        h2=(x2*h2%m+y2)%m;        if(h2==a2){            p2=i;            break;        }    }    for(int i=1;i<=m;i++){        h2=(x2*h2%m+y2)%m;        if(h2==a2){            q2=i;            break;        }    }    if(p1==-1 || p2==-1){        cout<<-1<<endl;        return 0;    }    for(int i=1;i<=2*m;i++){        if(p1==p2){            cout<<p1<<endl;            return 0;        }        if(p1<p2) p1+=q1;        else p2+=q2;    }    cout<<-1<<endl;    return 0;}


找出第一个h1==a1的时间p1,以及一个循环之后h1==a1的时间q1
0 0