Mike and Frog(CF547A)
来源:互联网 发布:ipad2越狱后必装软件 编辑:程序博客网 时间:2024/06/02 10:54
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.
54 21 10 12 3
3
10231 21 01 21 1
-1
In the first sample, heights sequences are following:
Xaniar:
Abol:
题意:就是说h1=(h1*x1+y1)%m;h2=(h2*x2+y2)%m;经过t次变化,问你能否使得h1==a1;h2==a2;如果可以就输出t;否则输出-1
思路:考虑循环节;情况一:不管变化几次都无法到达a1或a2;情况二:能到,但是a1或a2有一个不在,或者两个都不在循环节内;情况三:在循环节内,利用同余方程,扩展欧几里德求解;<此题有个大坑,那就是用欧几里德求解时候,x的值在运算过程中可能会溢出,要做特殊处理>
详见代码:
#include<stdio.h>#include<string.h>const int N = 1000006;#define LL __int64void exgcd(LL a,LL b,LL& d,LL& x,LL& y){ if(!b){d=a;x=1;y=0;} else { exgcd(b,a%b,d,y,x); y-=x*(a/b); }}bool vis1[N],vis2[N];int main(){ LL m,h1,h2,a1,a2,x1,x2,y1,y2; scanf("%I64d",&m); scanf("%I64d%I64d",&h1,&a1); scanf("%I64d%I64d",&x1,&y1); scanf("%I64d%I64d",&h2,&a2); scanf("%I64d%I64d",&x2,&y2); LL h=h1; memset(vis1,false,sizeof(vis1)); memset(vis2,false,sizeof(vis2)); bool bo=false; vis1[h1]=true; vis2[h2]=true; LL cnt1=0,tp1=0; LL cnt2=0,tp2=0; while(1) { h=(x1*h+y1)%m; if(!vis1[a1]) tp1++; if(vis1[h]) break; vis1[h]=true; } if(vis1[a1]) { memset(vis1,0,sizeof(vis1)); vis1[a1]=true; h=a1; cnt1=0; while(1) { h=(x1*h+y1)%m; cnt1++; if(vis1[h]) break; vis1[h]=true; } if(h!=a1) cnt1=0; h=h2; while(1) { h=(x2*h+y2)%m; cnt2++; if(!vis2[a2]) tp2++; if(vis2[h]) break; vis2[h]=true; } if(vis2[a2]) { memset(vis2,false,sizeof(vis2)); vis2[a2]=true; h=a2; cnt2=0; while(1) { h=(x2*h+y2)%m; cnt2++; if(vis2[h]) break; vis2[h]=true; } if(h!=a2) cnt2=0; if(cnt1==0||cnt2==0) { if(cnt1==0&&cnt2!=0) { LL tmp=tp1-tp2; if(tmp>=0&&tmp%cnt2==0) printf("%I64d\n",tp1); else printf("-1"); } else if(cnt2==0&&cnt1!=0) { LL tmp=tp2-tp1; if(tmp>=0&&tmp%cnt1==0) printf("%I64d\n",tp2); else printf("-1"); } else { if(tp1==tp2) printf("%I64d\n",tp1); else printf("-1\n"); } } else { if(tp1==tp2) printf("%I64d\n",tp1); else { LL a=cnt2; LL b=cnt1; LL c1=tp2; LL c2=tp1; LL c=c2-c1; LL d,x,y; exgcd(a,b,d,x,y); if(c%d) printf("-1\n"); else if(d==1&&x*y>0) { LL ax=1,by=1; while((ax*a%m+c1)!=(by*b%m+c2)) { if((ax*a%m+c1)-(by*b%m+c2)<0) ax++; else by++; } printf("%I64d\n",ax*a+c1); } else { x=x*(c/d); LL mm=b/d; x=(x%mm+mm)%mm; printf("%I64d\n",c1+a*x); } } } } else printf("-1\n"); } else printf("-1\n"); return 0;}
大神代码:
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;#define out(x) {printf("%I64d\n",(long long)(x));return 0;}int m,h1,a1,x1,y1,h2,a2,x2,y2,p,q,c,dx,dy;int main(){ scanf("%d",&m); scanf("%d%d%d%d",&h1,&a1,&x1,&y1); scanf("%d%d%d%d",&h2,&a2,&x2,&y2); for(p=1;p<=m;p++){ h1=(1LL*h1*x1+y1)%m; h2=(1LL*h2*x2+y2)%m; if(h1==a1)break; } if(p>m)out(-1); if(h1==a1&&h2==a2)out(p); for(c=1;c<=m;c++){ h1=(1LL*h1*x1+y1)%m; if(h1==a1)break; } if(c>m)out(-1); dx=1;dy=0; for(int i(1);i<=c;i++){ dx=(1LL*dx*x2)%m; dy=(1LL*dy*x2+y2)%m; } for(int i(1);i<=m;i++){ h2=(1LL*dx*h2+dy)%m; if(h2==a2)out(p+1LL*c*i); } out(-1); return 0;}
- Mike and Frog(CF547A)
- 【CF547A】Mike and frog
- Codeforces548C:Mike and Frog
- Codeforces548C:Mike and Frog
- C. Mike and Frog
- 【CodeForces】A. Mike and Frog
- codeforces 548 Mike and Frog
- CodeForces - 548C Mike and Frog(扩展欧几里德)
- codeforces 548 C Mike and Frog
- CF 547 A Mike and Frog
- Mike and Frog - CodeForces #547 A
- Codeforces Round #305 C. Mike and Frog
- #305 (div.2) C. Mike and Frog
- CF#305-C. Mike and Frog-数学模拟
- codeforces 548 C. Mike and Frog
- Codeforces #305 C. Mike and Frog 数论
- code forces 548C:Mike and frog
- CodeForces 547A Mike and Frog
- 不用+、-、×、÷对两个数求和
- [leetcode] 77.Combinations
- 个人笔记之构造方法
- Android 仿微信对话列表滑动删除效果
- 如何学习PostgreSql sql 语法
- Mike and Frog(CF547A)
- C++ int与string的转化
- 第二届SDN大赛--提高题2
- swift学习笔记-字符串和字符(Strings and Characters)
- Lowest price of film faced plywood
- 将指针强制类型转换为函数指针
- Struts 2标签
- 白话经典算法系列之二 直接插入排序的三种实现
- 一步步将vim改造成C/C++开发环境(IDE)