Codeforces Round #305 (Div. 2) C题 (数论)
2015-05-28 23:40
477 查看
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
output
input
output
此题由于模m,可以很容易想到循环节,然而我开始确实想得太简单了。 模m的循环里主要有以下情况
1)模m里的循环节不经过a
2)模m的只经过一次a,然后在另一个循环节里循环。
3)模m存在一个经过a的循环节。
对于前两种情况,记录下第一次经过a的时间。对于第二种情况,记录下第一次经过a的时间以及a的循环节的时间长度。
然后分情况讨论即可。
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; }
相关文章推荐
- 【吾日三省吾身】2015.5.28-涅槃行动第十天
- html初步 (一)
- asp.net Identity2 角色(Role)的使用(三)用户管理,用户控制器和视图
- 习题3-42
- 《Spring 2之站立会议3》
- JAVA基础笔记——String
- 学习STL过程中的一些零散记录1
- JavaScript动态增删改表格数据
- 面向接口对象
- poj 1068 Parencodings
- 浅析图标的微观世界――从符号学说起
- Android修改程序名称(包名)总结
- Python学习第三篇之pass的作用
- 磁盘与文件系统管理
- 19.Remove Nth Node From End of List
- curl发送post和get请求
- 线程安全和资源共享(Thread Safety and Shared Resources)
- 黑马程序员——多线程(三)
- 梦断代码阅读笔记
- 社会化海量数据采集爬虫框架搭建