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. POJ 2796 Feel Good(单调栈)

    传送门 Description Bill is developing a new mathematical theory for human emotions. His recent investig ...

  2. JAVA获取apk包的package和launchable-activity名称(一)

    背景: 每次要获取apk包的package和launchable-activity名称都需要运行doc命令,感觉好浪费感情,因为经常记不住常常的路径,但又不想把aapt设置为环境变量 我这个工具分几步 ...

  3. Linux学习一周初体验

    Linux一周初体验一.准备工欲善其事,必先利其器--虚拟机+Redhat7.0构成学习的环境.安装有条不紊.按部就班.......(涉及到的KVM.VNC.Root密码重置等内容,之后再详细了解)注 ...

  4. 常见linux命令释义(第六天)——shell环境变量

    太懒了,这几天好像得了懒癌,一点都不想写博客.后来想想,知识嘛,还是分享出来的好.第一治自己的懒癌:第二顺便巩固下自己的知识. Linux的变量分为两种,一种是系统变量,是系统一经启动,就写进内存中的 ...

  5. DNS(企业级)

    构建DNS(企业级) 1.硬件选型 CPU:12C以上配置 内存:16G 网络:千兆 2.初始化系统配置 关闭 iptables service iptables stop chkconfig ipt ...

  6. SaltStack与ZeroMQ(二)

    SaltStack与ZeroMQ SaltStack底层是基于ZeroMQ进行高效的网络通信. ZeroMQ简介 ØMQ (也拼写作ZeroMQ,0MQ或ZMQ)是一个为可伸缩的分布式或并发应用程序设 ...

  7. Arcgis 几何网络分析

    ArcGIS:网络分析(转)   由于之前对网络分析的理解有很多问题,所以在网上找了一些资料,这是其中一篇我觉得比较好的,所以就整理了一下,发到网上来,留个底吧,呵呵 注:关于几何网络的建立见前面的& ...

  8. 深入剖析z-index属性

    一.z-index七阶层叠顺序表 1.层叠顺序的大小比较: background/border < 负z-index < block块状水平盒子 < float浮动盒子 < i ...

  9. C#中使用DateTimePicker控件显示修改日期时间

    1.只显示日期   默认就是   2.只显示时间   修改属性 Format 设为Time ShowUpDown设为true   3.同时显示日期时间   Format设为Custom CustomF ...

  10. 《CSS3实战》读书笔记 第2章 层叠样式表(CSS)

    ## 层叠样式表 本章将阐述CSS的基本规则. ### 解构CSS 所谓CSS,由选择器(selector)和声明块(declaration block)组成.再进一步细分,每个声明包括了属性和值. ...