CF #305 (Div. 2) C. Mike and Frog(扩展欧几里得&&当然暴力is also no problem)

时间:2021-02-18 09:36:35
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.

CF #305 (Div. 2) C. Mike and Frog(扩展欧几里得&&当然暴力is also no problem)

So, if height of Xaniar is h1 and height of Abol is h2, after one second height of Xaniar will become CF #305 (Div. 2) C. Mike and Frog(扩展欧几里得&&当然暴力is also no problem) and height of Abol will become CF #305 (Div. 2) C. Mike and Frog(扩展欧几里得&&当然暴力is also no problem) where x1, y1, x2 and y2 are some integer numbers and CF #305 (Div. 2) C. Mike and Frog(扩展欧几里得&&当然暴力is also no problem) 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

CF #305 (Div. 2) C. Mike and Frog(扩展欧几里得&&当然暴力is also no problem)

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: CF #305 (Div. 2) C. Mike and Frog(扩展欧几里得&&当然暴力is also no problem)

 #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. 数论&sol;暴力 Codeforces Round &num;305 &lpar;Div&period; 2&rpar; C&period; Mike and Frog

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

  2. Codeforces Round &num;305 &lpar;Div&period; 1&rpar; A&period; Mike and Frog 暴力

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

  3. CF &num;305&lpar;Div&period;2&rpar; D&period; Mike and Feet&lpar;数学推导)

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

  4. set&plus;线段树 Codeforces Round &num;305 &lpar;Div&period; 2&rpar; D&period; Mike and Feet

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

  5. 暴力 Codeforces Round &num;305 &lpar;Div&period; 2&rpar; B&period; Mike and Fun

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

  6. 字符串处理 Codeforces Round &num;305 &lpar;Div&period; 2&rpar; A&period; Mike and Fax

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

  7. Codeforces Round &num;305 &lpar;Div&period; 2&rpar; B&period; Mike and Fun 暴力

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

  8. Codeforces Round &num;305 &lpar;Div&period; 2&rpar; A&period; Mike and Fax 暴力回文串

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

  9. Codeforces Round &num;305 &lpar;Div&period; 2&rpar; D&period; Mike and Feet 单调栈

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

随机推荐

  1. nodejs 编写(添加时间戳)命令行工具 timestamp

    Nodejs除了编写服务器端程序还可以编写命令行工具,如gulp.js就是Nodejs编写的. 接下来我们来实现一个添加时间戳的命令: $ timestamp action https://www.n ...

  2. Sqoop2搭建及使用

    1. 下载并安装配置Sqoop [需要的环境:Hadoop,Java] 首先  Hadoop版本2.7.2 20161013 找了篇Sqoop的文章就开撸  结果发现什么1.3,1.9,又有什么Sqo ...

  3. JS 日期格式化

    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"& ...

  4. windows 10 &amp&semi; Office 2016 安装

    Office 2016 VOL版    http://blog.sina.com.cn/s/blog_470614a90102vtmc.html 专业版合集: magnet:?xt=urn:btih: ...

  5. FastReport报表对象介绍一:&OpenCurlyDoubleQuote;Text”对象

    FastReport中文网 http://www.fastreportcn.com/Article/70.html ------------------------------------------ ...

  6. cocos2dx 内存管理机制

    持续更新吧. 刚开始看了一些. 一,CCObject  提供引用计数 1,unsinged int m_uReference; //此为CCOBject的引用计数,初始化为 1: new CCObje ...

  7. Mongodb集群配置&lpar;sharding with replica set&rpar;

    转自:http://blog.csdn.net/zhangzhaokun/article/details/6269514 前言 最近在研习MongoDB集群,找到一个不错的例子,加了几句,按照自己的理 ...

  8. 基于HTML5 SVG炫酷文字爆炸特效

    这是一款使用html5 svg.css3和js制作的炫酷文字爆炸特效.该文字特效用SVG path属性将文字路径切割为很多小块,然后使用css3和js在鼠标滑过文字时制作文字爆炸分裂的炫酷效果. 在线 ...

  9. SpringMVC常用配置&lpar;二&rpar;&comma;最简洁的配置实现文件上传

    Spring.SpringMVC持续介绍中,基础配置前面已经介绍了很多,如果小伙伴们还不熟悉可以参考这几篇文章: 1.Spring基础配置 2.Spring常用配置 3.Spring常用配置(二) 4 ...

  10. Xenserver7&period;6修改root密码

    一:重启xenserver服务器 进入此界面时,先用上下建随便动下,解除4S倒计时,后按e键