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 ...
随机推荐
- ACM_小G的循环
小G的循环 Time Limit: 2000/1000ms (Java/Others) Problem Description: 一回生,二回熟,三回就腻,小G用for,while循环用得太多了,累觉 ...
- 313 Super Ugly Number 超级丑数
编写一段程序来寻找第 n 个超级丑数.超级丑数是指其所有质因数都在长度为k的质数列表primes中的正整数.例如,[1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] ...
- Spring Boot (23) Lettuce Redis
Spring Boot除了支持常见的ORM框架外,更是对常用的中间件提供了非常好的封装,随着SpringBoot2.x的到来,支持的组件也越来越丰富,也越来越成熟,其中对Redis的支持不仅仅是它丰富 ...
- [ SHOI 2012 ] 随机树
\(\\\) \(Description\) 开始有一棵只有一个根节点的树.每次随机选择一个叶子节点,为他添上左右子节点,求: 生成一棵有\(N\)个叶节点的树,所有叶节点平均高度的期望. 生成一棵有 ...
- ThinkPHP的基础使用
最近学习了ThinkPHP框架写页面,趁着夜色写写自己的一些经验: 我这里用的服务器是phpStudy,数据库是Navicat,项目必须放在phpStudy的WWW目录里面. 1,搭建th项目 1.将 ...
- 将子节点的所有父节点ID合并成一个字符串,并更新表
begin for cur_dept in (select SLCATALOG_ID from T_GIS_SLCATALOG) loop UPDATE T_GIS_SLCATALOG SET PAT ...
- 实现X*N
#include<iostream> using namespace std; double foo(int n,double x) { if(1==n) { return x; } el ...
- 图解TCP/IP笔记(3)——IP协议
目录 IP协议 IP寻址 IP地址组成 IP地址分类 广播地址 子网掩码 全局地址和私有地址 IP协议 跨越不同数据链路,实现两端节点之间的数据包传输 数据链路:只负责某一个区间之间的通信传输 IP协 ...
- DeltaFish 校园物资共享平台 第八次小组会议
DeltaFish 校园物资共享平台 第八次小组会议 记录人:娄雨禛 2018.7.31 会议总结 1. 对前端界面进行改进,具体改进内容如下: 2. 后端从登录和注册的具体实现做起,熟悉流程之后完成 ...
- Gpupdate命令详解
刷新本地和基于 Active Directory 的组策略设置,包括安全设置.该命令可以取代 secedit 命令中已经过时的 /refreshpolicy 选项. MS-DOS命令语法 gpupda ...