codeforces 548 C. Mike and Frog

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


思路:
細節挺多的一道題,原題可以轉化爲a * x + b = c * y + d,然後枚舉x,計算y,取min(a * x + b)就好了,x只要枚舉m次
/*======================================================
# Author: whai
# Last modified: 2015-12-09 12:36
# Filename: c1.cpp
======================================================*/
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <stack>

using namespace std;

#define LL __int64
#define PB push_back
#define P pair<int, int>
#define X first
#define Y second


int m;

void get_AB(int h, int a, int x, int y, LL &A, LL &B) {
	LL tmp = h;
	A = 0;
	for(int i = 1; i <= m; ++i) {
		tmp = (tmp * x + y) % m;
		++A;
		if(tmp == a) break;
	}
	if(tmp != a) ++A;
	tmp = a;
	B = 0;
	for(int i = 1; i <= m; ++i) {
		tmp = (tmp * x + y) % m;
		++B;
		if(tmp == a) break;
	}
	if(tmp != a) ++B;
}

int main() {
	int h[2], a[2], x[2], y[2];
	cin>>m;
	
	for(int i = 0; i < 2; ++i) {
		cin>>h[i]>>a[i]>>x[i]>>y[i];
	}
	int flag = 0;
	if(h[0] == a[0] && h[1] == a[1]) {
		flag = 1;
		puts("0");
	} else {
		LL A[2], B[2];
		for(int i = 0; i < 2; ++i) {
			get_AB(h[i], a[i], x[i], y[i], A[i], B[i]);
		}
		//cout<<A[0]<<' '<<B[0]<<' '<<A[1]<<' '<<B[1]<<endl;
		if(A[0] == m + 1 || A[1] == m + 1) {
		
		} else if(A[0] == A[1]) {
			cout<<A[0]<<endl;
			flag = 1;
		} else if(B[0] == m + 1 || B[1] == m + 1) {
			if(B[0] != m + 1 || B[1] != m + 1) {
				if(B[0] != m + 1) {
					swap(A[0], A[1]);
					swap(B[0], B[1]);
				}
				if(A[0] >= A[1] && (A[0] - A[1]) % B[1] == 0) {
					cout<<A[0]<<endl;
					flag = 1;
				}
			}
		} else {
			for(int i = 0; i <= m; ++i) {
				LL left = A[0] + i * B[0];
				if(left >= A[1]) {
					if((left - A[1]) % B[1] == 0) {
						flag = 1;
						cout<<left<<endl;
						break;
					}
				}
			}
		}
	}
	if(!flag) puts("-1");

	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章