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
此题由于模m,可以很容易想到循环节,然而我开始确实想得太简单了。 模m的循环里主要有以下情况
1)模m里的循环节不经过a
2)模m的只经过一次a,然后在另一个循环节里循环。
3)模m存在一个经过a的循环节。
对于前两种情况,记录下第一次经过a的时间。对于第二种情况,记录下第一次经过a的时间以及a的循环节的时间长度。
然后分情况讨论即可。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define LL long long
using namespace std; int vist[1000007]; void gao(LL h,LL a,LL x,LL y,LL &t,LL &l,LL m){
t=l=0;
memset(vist,-1,sizeof(vist));
vist[h]=0;
while(true){
// cout<<x*h+y<<endl;
h=(x*h+y)%m;
// cout<<h<<endl;
l++;
if(vist[h]>-1){
l=t=-1;
return ;
}
vist[h]=l;
if(h==a){ break;
}
}
// cout<<"YES"<<endl;
LL tmp=l; t=l;
while(true){
h=(x*h+y)%m;
l++;
if(vist[h]>tmp){
t=-1; break;
}
vist[h]=l;
if(h==a) {
break;
}
}
if(t!=-1) l=l-tmp;
else l=tmp;
} LL gcd(LL a,LL b){
if(b==0) return a;
return gcd(b,a%b);
} int main(){
LL m,a1,h1,x1,y1,a2,h2,y2,x2,t1,l1,t2,l2;
// while(scanf("%I64d",&m)!=EOF){
cin>>m;
// scanf("%d%d%d%d",&h1,&a1,&x1,&y1);
cin>>h1>>a1>>x1>>y1;
gao(h1,a1,x1,y1,t1,l1,m);
// scanf("%d%d%d%d",&h2,&a2,&x2,&y2);
cin>>h2>>a2>>x2>>y2;
gao(h2,a2,x2,y2,t2,l2,m);
// printf("l1=%lld t1=%lld l2=%lld t2=%lld\n",l1,t1,l2,t2);
if(t1==-1&&t2==-1){
if(l1==l2)//printf("%d\n",l1);
cout<<l1<<endl;
else puts("-1");
}
else if(t1==-1||t2==-1){
if(l1==-1||l2==-1) puts("-1");
else{
// if(l1==l2) cout<<l1<<endl;
if(t1==-1){
if(l1<t2) puts("-1");
else if((l1-t2)%l2==0){
// printf("%d\n",(l1-t2)/l2);
cout<<l1<<endl;
}
else puts("-1");
}
else if(t2==-1){
if(l2<t1) puts("-1");
else if((l2-t1)%l1==0){
// printf("%d\n",(l2-t1)/l1);
cout<<l2<<endl;
}
else puts("-1");
}
}
}
else{
if(t1>t2){
swap(t1,t2),swap(l1,l2);
}
// printf("l1=%lld t1=%lld l2=%lld t2=%lld\n",l1,t1,l2,t2);
if(abs(t2-t1)%gcd(l2,l1)!=0) puts("-1");
else{
int k=0;
while(true){
if((t2+k*l2-t1)%l1==0) break;
k++;
}
// printf("%d\n",t2+k*l2);
cout<<t2+k*l2<<endl;
}
}
// }
return 0;
}

  

Codeforces Round #305 (Div. 2) C题 (数论)的更多相关文章

  1. Codeforces Round #305 (Div. 2) E题(数论+容斥原理)

    E. Mike and Foam time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  2. Codeforces Round #305 (Div. 2) D题 (线段树+RMQ)

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

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

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

  4. Codeforces Round #378 (Div. 2) D题(data structure)解题报告

    题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...

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

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

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

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

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

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

  8. Codeforces Round #612 (Div. 2) 前四题题解

    这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...

  9. Codeforces Round #713 (Div. 3)AB题

    Codeforces Round #713 (Div. 3) Editorial 记录一下自己写的前二题本人比较菜 A. Spy Detected! You are given an array a ...

随机推荐

  1. AcWing算法基础1.2

    排序 归并排序 归并排序和快速排序相反,快排是先排后分再合并,归并则是先分后排再合并 归并排序时间复杂度是O(n logn) 分析:    ------------------------------ ...

  2. nginx + php-fpm 运行原理

    一.关于nginx 1.1 简单认知 我们都知道nginx 是web服务器. 也知道 用户访问时通过ip和端口访问 nginx. 那么nginx 是如何 通过php 获取数据并返回数据的呢? 1.2 ...

  3. 【转】Linux命令学习手册-split命令

    转自:http://blog.chinaunix.net/uid-9525959-id-3054325.html split [OPTION] [INPUT [PREFIX]] [功能]将文件分割成多 ...

  4. C#中 分层 显示数据库中多表的数据信息

    如下图,要实现将三个表中的内容加载到同一个窗体中,该怎么来实现呢? 要实现上面的查询结果,我们就要从Student表中拿到学生姓名,从Subject表中拿到科目名称,从StudentResult表中拿 ...

  5. mysql GTID主从复制(主库在线,添加新丛库)

    要求: 1.         主库上线,主库不停止服务的前提下做主从复制 2.         新添加一个丛库 操作: 1.         在主库导出数据(主库正常运行): 2.         将 ...

  6. SQLSERVER SQL性能优化技巧

    这篇文章主要介绍了SQLSERVER SQL性能优化技巧,需要的朋友可以参考下 1.选择最有效率的表名顺序(只在基于规则的优化器中有效)       SQLSERVER的解析器按照从右到左的顺序处理F ...

  7. Ajax——php基础知识(一)

    AMP环境 AMP(Apache.MySQL.PHP)是三个独立的软件,但是对于初学者而言分别安装以及配置需要掌握一定的软件知识,所以就有了很多AMP集成环境帮助我们简化安装 ——WAMP WAMP安 ...

  8. 学习笔记-java IO流总结 转载

    1.什么是IO Java中I/O操作主要是指使用Java进行输入,输出操作. Java所有的I/O机制都是基于数据流进行输入输出,这些数据流表示了字符或者字节数据的流动序列.Java的I/O流提供了读 ...

  9. java攻城师之路--复习java web之servlet

    需要掌握的知识点:1.Servlet程序编写 ----- 生命周期2.ServletAPI Request Response 3.Cookie 和 Session Servlet 用来 动态web资源 ...

  10. The type new View.OnClickListener(){} must implement the inherited abstract method View.Onclicklis

    public class MainActivity extends Activity { protected Button startBrew = null; @Override protected ...