Codeforces Round #305 (Div. 2) C题 (数论)
1 second
256 megabytes
standard input
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.
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.
Print the minimum number of seconds until Xaniar reaches height a1 and Abol reaches height a2 or print -1 otherwise.
5
4 2
1 1
0 1
2 3
3
1023
1 2
1 0
1 2
1 1
-1
#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题 (数论)的更多相关文章
- 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 ...
- 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 ...
- 数论/暴力 Codeforces Round #305 (Div. 2) C. Mike and Frog
题目传送门 /* 数论/暴力:找出第一次到a1,a2的次数,再找到完整周期p1,p2,然后以2*m为范围 t1,t2为各自起点开始“赛跑”,谁落后谁加一个周期,等到t1 == t2结束 详细解释:ht ...
- Codeforces Round #378 (Div. 2) D题(data structure)解题报告
题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...
- set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet
题目传送门 /* 题意:对于长度为x的子序列,每个序列存放为最小值,输出长度为x的子序列的最大值 set+线段树:线段树每个结点存放长度为rt的最大值,更新:先升序排序,逐个添加到set中 查找左右相 ...
- 暴力 Codeforces Round #305 (Div. 2) B. Mike and Fun
题目传送门 /* 暴力:每次更新该行的num[],然后暴力找出最优解就可以了:) */ #include <cstdio> #include <cstring> #includ ...
- 字符串处理 Codeforces Round #305 (Div. 2) A. Mike and Fax
题目传送门 /* 字符串处理:回文串是串联的,一个一个判断 */ #include <cstdio> #include <cstring> #include <iostr ...
- Codeforces Round #612 (Div. 2) 前四题题解
这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...
- Codeforces Round #713 (Div. 3)AB题
Codeforces Round #713 (Div. 3) Editorial 记录一下自己写的前二题本人比较菜 A. Spy Detected! You are given an array a ...
随机推荐
- Kubernetes 配置 Taint 和 Toleration(污点和容忍)
通过污点和容忍让pod运行在特定节点上 参考官网:https://k8smeetup.github.io/docs/concepts/configuration/taint-and-toleratio ...
- wcf 代理配置
<?xml version="1.0" encoding="utf-8"?> <configuration> <appSett ...
- 通过Oracle透明网关连接Sybase
Oracle公司提出的透明网关技术可用于实现与其他多种类型的数据库的互联,实现不同类型数据之间建立连接,方便于使用者进行查询.近日,在公司的某项目的实施过程中,开发人员需要访问Sybase数据库中的某 ...
- 关于BUG
1.BUG的理解 2.提高BUG report的技巧
- Unity学习-工具准备(一)
工具介绍 Unity 4.5.4 VS2013 Visual Studio 2013 Tools for Unity unity3d圣典 五大面板 Hierarchy:场景资源面板 [管理 当前场景 ...
- Spring.net(v1.3.2) 官网API-第一章 前言
虽然有很好的工具和技术,但是开发软件应用仍然是很困难的.Spring为构建企业级应用提供了一个轻量级的解决方案,Spring提供了一个统一的.透明的方式去配置你的应用,和将AOP集成到你的软件中.Sp ...
- iOS keychain入门
学了很久的iOS,一直都是明文保存用户名和密码在本地,手机一般都是自己用的,而且非越狱手机东西也不怎么能拿到数据,所以也就没在乎那么多,当然,这是不科学的.悄悄的说,这块一直不是我写的~~~ 用户隐私 ...
- css+background实现 图片宽高自适应,拉伸裁剪不变形
图片宽高不固定 ,一样实现自适应,拉伸裁剪不变形,适应各大兼容性. 下面咱们在网上找两张宽高不一样的照片: No.1 ...
- CSS——样式隐藏
overflow:hidden: 溢出隐藏 visibility:hidden: 隐藏元素 隐藏之后还占据原来的位置. display:none: 隐藏元素 隐藏之后不占据 ...
- MySQL主从备份配置
MySQL主从热备配置 两台服务器的MySQL版本都是5.5.41master:192.168.3.119slave:192.168.3.120 MySQL主服务器配置:1.创建用于备份的用户 gra ...