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. flex中align-content属性

    在flex弹性盒模型中align-content属性控制容器内多行在交叉轴上的排列方式 默认值:stretch 可用值: 属性值:flex-start 属性值:flex-end 属性值:center ...

  2. T-SQL查询基础

    今天来带大家了解下在sql server 中的查询机制 使用select语句进行查询 1.查询所有的数据行和列 select * from student 2.查询部分行和列 select scode ...

  3. 来自一个用户的体验-Alpha项目测试

    软件梦之队成员:201731062305 周蓉 这个作业属于哪个课程 <课程的链接> 这个作业要求在哪里 <作业要求的链接> 团队名称 <软件梦之队>(附上团队博客 ...

  4. html5——动画案例(时钟)

    1.秒钟转360度需要60s分60步 2.分针转360度需要3600s分60步 3.秒钟转360度需要43200s分60步 <!DOCTYPE html> <html lang=&q ...

  5. [Windows Server 2012] 手工创建安全网站

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:手工创建安全站 ...

  6. UICollectionView(一)基本概念

    整体预览 高等级的包含和管理(Top-level containment and management) UICollectionView UICollectionViewController UIC ...

  7. QQ空间里写的开发心得

    不回头看一眼还真没发现我已经写过这么多开发心得日志. 理一理设备数据走向  https://user.qzone.qq.com/1156740846/blog/1522292793 action的生命 ...

  8. PHP 设计模式--序言

    面向对象是PHP5之后增加的功能,是PHP走向现代语言的一个标志. 在过程式设计时代,PHP以学习成本低.入门快的特点赢得很多WEB开发者的青睐,但同时也限制了PHP的发展. 借鉴Java和C++之后 ...

  9. CAD得到自定义实体拖放夹点(com接口VB语言)

    主要用到函数说明: MxDrawXCustomEvent::MxDrawXCustomEntity::getGripPoints 自定义实体事件,得到拖放夹点,详细说明如下: 参数 说明 LONGLO ...

  10. parseInt()函数

    parseInt()转化整形是从左往右,取出第一个整型,如:10a10b,则显示10: //20170719补充 如果该字符串第一个字符是0,那么该字符串会基于八进制而非十进制来求值,在八进制中,8和 ...