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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值