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 ...
随机推荐
- NS2学习笔记(五)
对无线网络,生成nam文件要使用namtrace-all-wireless, 而不是namtrace-all: set nf [open test_1.nam w] $ns_ namtrace-all ...
- 【HTML5】基于HTML5的高性能动画与游戏
其实这篇文章类似版本早在12年就在网上各处出现了,也随着HTML5的兴起,HTML的新特性也是倍受开发者们追捧,自然相关HTML5的高性能动画与游戏的相关文章也是层出不穷的,笔者也是在12年接触的相关 ...
- BZOJ 4867 分块+神tm卡常
思路: 注意到len<=10 按照权值max-min<=sqrt(n)*len 分块 记一下前缀和 每修改sqrt(n)次以后重新分块 修改的时候整块打标记 两边重构 (这题常数卡 ...
- 【洛谷4396/BZOJ3236】[AHOI2013]作业(莫队+分块/树状数组/线段树)
题目: 洛谷4396 BZOJ3236(权限) 这题似乎BZOJ上数据强一些? 分析: 这题真的是--一言难尽 发现题面里没说权值的范围,怕出锅就写了离散化.后来经过面向数据编程(以及膜神犇代码)知道 ...
- SQL Sever语言 存储过程及触发器
存储过程:就像函数一样的会保存在数据库中-->可编程性-->存储过程 创建存储过程: 保存在数据库表,可编程性,存储过程create proc jiafa --需要的参数@a int,@b ...
- 我正在学英语是用learning english还是用studying english?
学一门语言用 learn. study 表示深入研究,一般指在大学里.如果大学里的专业是英语,就可以说 study English. 1. If you study hard, you will le ...
- AdminLTE介绍和zTree的简单使用
一.AdminLTE介绍 1.介绍 AdminLTE是一个开源的后台控制面板和仪表盘 WebApp 模板,是建立在Bootstrap3框架和JQuery之上的开源模板主题工具,它提供了一系列响应的 ...
- [ CTSC 2007 / BZOJ 2151 ] Backup / 种树
\(\\\) \(Description\) 给出一个数轴上\(N\)个点的坐标\(A_i\),选择\(K\)个点对,使得这\(K\)个点对每个点对的距离之和尽可能小. \(N\in [0,10^5] ...
- Python之IPython开发实践
Python之IPython开发实践 1. IPython有行号. 2. Tab键自动完成,当前命名空间任何与已输入字符串相匹配的变量就会被找出来. 3. 内省机制,在变量前或者后面加上(?)问号,就 ...
- centos7下手动制作trove镜像
获取镜像 [root@bldattet1 ~]# wget http://mirrors.aliyun.com/centos/7.5.1804/isos/x86_64/CentOS-7-x86_64 ...