博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #305 (Div. 2) C题 (数论)
阅读量:4689 次
发布时间:2019-06-09

本文共 2819 字,大约阅读时间需要 9 分钟。

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 
#include
#include
#include
#define LL long longusing 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<
<
-1){ l=t=-1; return ; } vist[h]=l; if(h==a){ break; } }// cout<<"YES"<
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<
<
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<
<

  

转载于:https://www.cnblogs.com/jie-dcai/p/4537358.html

你可能感兴趣的文章
数组 Arrays类
查看>>
String类的深入学习与理解
查看>>
OLTP vs OLAP
查看>>
一些题目(3)
查看>>
Eclipse下配置主题颜色
查看>>
杂题 UVAoj 107 The Cat in the Hat
查看>>
ftp sun jdk自带
查看>>
python 元类——metaclass
查看>>
什么是缓冲,引入缓冲的原因是什么?
查看>>
叙述下列术语的定义并说明它们之间的关系:卷、块、文件、记录。
查看>>
不把DB放进容器的理由
查看>>
OnePage收集
查看>>
Gym - 101492I 区间限制费用流
查看>>
Entity Framework技巧系列之九 - Tip 35 - 36
查看>>
2808 sci 接收中断
查看>>
原创:机器学习排序深入解读
查看>>
HDU 4288
查看>>
程序的跳转(一行代码)
查看>>
hello world ,详解
查看>>
Update
查看>>