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

题解:

In this editorial, consider p = ma = h1a′ = a1b = h2 and b′ = a2.

First of all, find the number of seconds it takes until height of Xaniar becomes a′ (starting from a) and call it q. Please note that q ≤ pand if we don't reach a′ after p seconds, then answer is  - 1.

If after q seconds also height of Abol will become equal to b′ then answer if q.

Otherwise, find the height of Abdol after q seconds and call it e.

Then find the number of seconds it takes until height of Xaniar becomes a′ (starting from a′) and call it c. Please note that c ≤ p and if we don't reach a′ after p seconds, then answer is  - 1.

if g(x) = Xx + Y, then find f(x) = g(g(...(g(x)))) (c times). It is really easy:

c = 1, d = 0
for i = 1 to c
c = (cX) % p
d = (dX + Y) % p

Then,

f(x)
return (cx + d) % p

Actually, if height of Abol is x then, after c seconds it will be f(x).

Then, starting from e, find the minimum number of steps of performing e = f(e) it takes to reach b′ and call it o. Please note thato ≤ p and if we don't reach b′ after p seconds, then answer is  - 1.

Then answer is x + c × o.

Time Complexity: 

 #include<stdio.h>
#include<string.h>
#include<algorithm>
typedef long long ll ;
ll mod ;
ll a , a1 ;
ll x , y ;
ll b , b1 ;
ll _x , _y ;
ll A , T ;
ll B , _T ; ll exgcd (ll a,ll b,ll& x,ll &y)
{
if(b==){
x=;
y=;
return a;
}
ll d = exgcd ( b , a % b , x , y ) ;
ll tmp = x ;
x = y ;
y = tmp - a / b * y ;
return d;
}
//用扩展欧几里得算法解线性方程ax+by=c;
void __exgcd(ll a , ll b , ll c )
{
ll x , y ;
ll d = exgcd ( a , b , x , y ) ;
if(c % d) {
puts ("-1") ;
return ;
} ll k = c / d ;
x *= k ; y *= k ;//求的只是其中一个解
if (d < ) d = -d ;
ll t1 = T / d , t2 = _T / d ;
// printf ("t1 = %I64d , t2 = %I64d\n" , t1 , t2 ) ;
//printf ("x = %I64d , y = %I64d\n" , x , y ) ;
if (x < || y < ) {
while (x < || y < ) {
x += t1 ;
y += t2 ;
}
}
else {
while (x >= && y >= ) {
x -= t1 ;
y -= t2 ;
}
x += t1 ;
y += t2 ;
}
//printf ("x = %I64d , y = %I64d\n" , x , y ) ;
printf ("%I64d\n" , A + T * y) ;
} bool workA ()
{
ll cnt = ;
ll c = a ;
while () {
cnt ++ ;
c = (c * x + y) % mod ;
if (c == a1) {
A = cnt ;
return true ;
}
if (cnt > mod) break ;
}
return false ;
} bool workB ()
{
ll cnt = ;
ll c = b ;
while () {
cnt ++ ;
c = (c * _x + _y) % mod ;
if (c == b1) {
B = cnt ;
return true ;
}
if (cnt > mod) break ;
}
return false ;
} bool workT ()
{
T = ;
ll cnt = ;
ll c = a1 ;
while () {
cnt ++ ;
c = (c * x + y) % mod ;
if (c == a1) {
T = cnt ;
return true ;
}
if (cnt > mod) break ;
}
return false ;
} bool work_T ()
{
_T = ;
ll cnt = ;
ll c = b1 ;
while () {
cnt ++ ;
c = (c * _x + _y) % mod ;
if (c == b1) {
_T = cnt ;
return true ;
}
if (cnt > mod) break ;
}
return false ;
} int main ()
{
//freopen ("a.txt" , "r" , stdin ) ;
while (~ scanf ("%I64d" , &mod)) {
scanf ("%I64d%I64d%I64d%I64d" , &a , &a1 , &x , &y) ;
scanf ("%I64d%I64d%I64d%I64d" , &b , &b1 , &_x , &_y) ;
if (!workA ()) puts ("-1") ;
else {
if (!workB ()) puts ("-1") ;
else if (A == B) printf ("%I64d\n" , A) ;
else {
workT () ;
work_T () ;
if (T == || _T == ) {
if (T == && _T == ) puts ("-1") ;
else if (T == ) {
if (A - B >= && (A - B) % _T == ) printf ("%I64d\n" , A) ;
else puts ("-1") ;
}
else if (_T == ) {
if (B - A >= && (B - A) % T == ) printf ("%I64d\n" , B) ;
else puts ("-1") ;
}
} else {
ll a = _T , b = -T , c = A - B ;
__exgcd (a , b , c) ;
}
}
}
}
return ;
}

题解:

一般情况:我们能用暴力求出a-->a1所需时间A , b-->b1所需时间B,a1-->a1时间Ta , b1-->b1时间Tb;

注意:A , B , Ta , Tb 都会在 “mod 时间”内完成,若没在这段时间内找到,则不存在。

所以为了达到目标显然需要满足一下等式:A + x * Ta = B + y * Tb ---- ①---> A - B = - Ta * x + Tb * y ----②;

那么问题就转变成了求该方程是否有整数解(这道题有整数解,就必有正整数解)。

根据扩展欧几里得算法:

      c = a * x + b * y ;

只要满足gcd (a , b) | c (及a,b的gcd为c的约数)时,该方程必有解,我们令 d = gcd (a , b) ;

则存在解时:(下面的_x , _y都假定为执行完 exgcd(a , b , x , y)后产生的 _x , _y)

其中一组特解:x' = _x * c / d ;(c = A - B)

       y' = _y * c / d ;

递推式:

    x = x' + Ta / fabs (d)  * k ;( k 为 参数)

    y = y' + Tb / fabs (d)  * k ;

然后我们只要暴力求出可行解(x , y)中与0最接近的即可 , A + x * Ta 及为答案。

暴力枚举:(简洁,大神的)

 #include <cstring>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <math.h>
#include <ctime>
#include <algorithm>
#include <vector>
#include <set>
#include <list>
#include <climits>
#include <cctype>
#include <bitset>
#include <iostream>
#include <complex> using namespace std; typedef stringstream ss;
typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<vector<ii> > vii;
typedef vector<string> vs;
typedef vector<ll> vi;
typedef vector<double> vd;
typedef long double ld;
typedef vector<vector<ll> > matrix;
typedef complex<double> point; #define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define sz(v) ((ll)v.size())
#define clr(v, d) memset(v, d, sizeof(v))
#define polar(r,t) ((r)*exp(point(0,(t))))
#define length(a) hypot((a).real(),(a).imag())
#define angle(a) atan2((a).imag() , (a).real())
#define vec(a,b) ((b)-(a))
#define dot(a,b) ((conj(a)*(b)).real())
#define cross(a,b) ((conj(a)*(b)).imag())
#define lengthSqr(a) dot(a,a)
#define rotate(v,t) ((v)*exp(point(0,t)))
#define rotateAbout(v,t,a) (rotate(vec(a,v),t)+(a))
#define reflect(v,m) (conj((v)/(m))*m)
#define dist(a,b) (sqrt(pow((a).real()-(b).real(),2.0)+pow((a).imag()-(b).imag(),2.0)))
#define normalize(a) ((a)/length(a)) int dx[] = { , -, , };
int dy[] = { , , , - };
double PI = 3.1415926535897932384626433832795; const ll oo = (ll) 1e9 + ;
const double eps = 1e-;
const ll mod = ; int main() {
//freopen("a.txt", "r", stdin);
ios_base::sync_with_stdio();
ll m, h1, a1, x1, y1, h2, a2, x2, y2;
cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;
ll idx1 = -, idx2 = -;
for (int i = ; i <= m; i++) {
h1 = (x1 * h1 + y1) % m;
if (h1 == a1) {
idx1 = i;
break;
}
}
for (int i = ; i <= m; i++) {
h2 = (x2 * h2 + y2) % m;
if (h2 == a2) {
idx2 = i;
break;
}
} if (idx1 == - || idx2 == -) {
cout << "-1" << endl;
return ;
} ll step1, step2;
for (int i = ; i <= m; i++) {
h1 = (x1 * h1 + y1) % m;
if (h1 == a1) {
step1 = i;
break;
}
}
for (int i = ; i <= m; i++) {
h2 = (x2 * h2 + y2) % m;
if (h2 == a2) {
step2 = i;
break;
}
}
//printf ("idx1 = %I64d , idx2 = %I64d , step1 = %I64d, step2 = %I64d\n" , idx1 , idx2 , step1 , step2 ) ;
for (int i = ; i <= * m; i++) {
if (idx1 == idx2) {
cout << idx1 << endl;
return ;
}
if (idx1 > idx2) {
idx2 += step2;
} else {
idx1 += step1;
}
}
cout << "-1" << endl;
return ;
}

CF #305 (Div. 2) C. Mike and Frog(扩展欧几里得&&当然暴力is also no problem)的更多相关文章

  1. 数论/暴力 Codeforces Round #305 (Div. 2) C. Mike and Frog

    题目传送门 /* 数论/暴力:找出第一次到a1,a2的次数,再找到完整周期p1,p2,然后以2*m为范围 t1,t2为各自起点开始“赛跑”,谁落后谁加一个周期,等到t1 == t2结束 详细解释:ht ...

  2. Codeforces Round #305 (Div. 1) A. Mike and Frog 暴力

     A. Mike and Frog Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/547/pr ...

  3. CF #305(Div.2) D. Mike and Feet(数学推导)

    D. Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  4. set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet

    题目传送门 /* 题意:对于长度为x的子序列,每个序列存放为最小值,输出长度为x的子序列的最大值 set+线段树:线段树每个结点存放长度为rt的最大值,更新:先升序排序,逐个添加到set中 查找左右相 ...

  5. 暴力 Codeforces Round #305 (Div. 2) B. Mike and Fun

    题目传送门 /* 暴力:每次更新该行的num[],然后暴力找出最优解就可以了:) */ #include <cstdio> #include <cstring> #includ ...

  6. 字符串处理 Codeforces Round #305 (Div. 2) A. Mike and Fax

    题目传送门 /* 字符串处理:回文串是串联的,一个一个判断 */ #include <cstdio> #include <cstring> #include <iostr ...

  7. Codeforces Round #305 (Div. 2) B. Mike and Fun 暴力

     B. Mike and Fun Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/548/pro ...

  8. Codeforces Round #305 (Div. 2) A. Mike and Fax 暴力回文串

     A. Mike and Fax Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/548/pro ...

  9. Codeforces Round #305 (Div. 2) D. Mike and Feet 单调栈

    D. Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

随机推荐

  1. WebConfig节点详解

    <!-- Web.config配置文件详解(新手必看) 花了点时间整理了一下ASP.NET Web.config配置文件的基本使用方法. 很适合新手参看,由于Web.config在使用很灵活,可 ...

  2. AngularJs 键盘事件和鼠标事件

    ngKeydown/ngKeypress/ngKeyup 该指令在按键按下/按键按下/按键松开时执行指定表达式. ngKeydown和ngKeypress略有不同,目前的测试是ngKeypress针对 ...

  3. python标准模块(一)

    本文会涉及到的模块: time datetime sys os random re hashlib 模块,用若干代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能 ...

  4. 3秒后自动跳转页面【js】

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. SOCKADDR_IN

    在windows/linux下有下面结构: sockaddr结构 struct sockaddr { unsigned short sa_family;/*addressfamily,AF_xxx*/ ...

  6. Runner站立会议01

    开会时间:9.10-9.30 地点:二教 今天做了什么:学了文本输入,button按钮,界面转换(意图) 遇到什么困难:界面转换时,出现问题,没有正常跳转 明天打所作什么:解决今天遇到的问题,学了第二 ...

  7. Java数据结构——用双端链表实现队列

    //================================================= // File Name : LinkQueue_demo //---------------- ...

  8. UITextField限制输入文字

    一.viewDidLoad时监听通知 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; [center addO ...

  9. adapter(转自Devin Zhang)

    1.概念 Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带.在常见的View(ListView,GridView)等地方都需要用到Adapter.如下图直 ...

  10. Docker Compose to CoreOS

    taken from https://docs.docker.com/compose/install/ the only thing is that /usr is read only, but /o ...