题意:给出h1,变换成下一个是x1*h1+y1模m,目的要得到a1,h2同理,2个数字是同时变换的,问同时得到a1和a2的最近时间,得不到输出-1。
做法:我们可以暴力找出a1第一次出现的位置l1,以及a2第一次出现的位置l2,因为m不超过10^6,所以循环节的长度不会超过10^6,然后再找出第二个a1出现的位置,以及第二个a2出现的位置,那么循环节的长度分别是w1,和w2。可能会出现w1和w2为0的情况,这种是因为陷入到一个未含有a1的循环节中了。这种情况分类讨论特判一下即可。
我们来看w1和w2不为0的情况。可以列出方程l1+x*w1 = l2+y*w2,我们要找的就是满足x>=0且y>=0的第一组解,w1*x - w2*y = l2-l1,用扩展欧几里德解出一组解即可,再通过常用的找其他解的方法得到答案。
AC代码:
#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdlib>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<string.h>
#include<string>
#include<sstream>
#include<bitset>
using namespace std;
#define ll __int64
#define ull unsigned long long
#define eps 1e-8
#define NMAX 10000000
#define MOD 1000000007
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1)
#define mp make_pair
template<class T>
inline void scan_d(T &ret)
{
char c;
int flag = 0;
ret=0;
while(((c=getchar())<'0'||c>'9')&&c!='-');
if(c == '-')
{
flag = 1;
c = getchar();
}
while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
if(flag) ret = -ret;
}
void 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);}
}
ll gcd(ll a, ll b)
{
return b == 0 ? a : gcd(b,a%b);
}
int main()
{
#ifdef GLQ
freopen("input.txt","r",stdin);
// freopen("o.txt","w",stdout);
#endif
ll m,h1,a1,x1,y1,h2,a2,x2,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 l1 = -1,l2 = -1,w1 = 0,w2 = 0;
for(int i = 1; i <= 2000000; i++)
{
h1 = (x1*h1+y1)%m;
if(h1 == a1 && l1 == -1) l1 = i;
else if(h1 == a1)
{
w1 = i-l1;
break;
}
}
if(l1 == -1)
{
printf("-1\n");
return 0;
}
for(int i = 1; i <= 2000000; i++)
{
h2 = (x2*h2+y2)%m;
if(h2 == a2 && l2 == -1) l2 = i;
else if(h2 == a2)
{
w2 = i-l2;
break;
}
}
if(l2 == -1)
{
printf("-1\n");
return 0;
}
if(w1 == 0 && w2 == 0)
{
if(l1 == l2) printf("%I64d\n",l1);
else printf("-1\n");
return 0;
}
else if(w1 == 0)
{
if(l1 >= l2 && (l1-l2)%w2 == 0) printf("%I64d\n",l1);
else printf("-1\n");
return 0;
}
else if(w2 == 0)
{
if(l2 >= l1 && (l2-l1)%w1 == 0) printf("%I64d\n",l2);
else printf("-1\n");
return 0;
}
if((l2-l1)%gcd(w1,w2) != 0)
{
printf("-1\n");
return 0;
}
ll g,x,y;
exgcd(w1,-w2,g,x,y);
// cout<<l1<<" "<<l2<<" "<<w1<<" "<<w2<<endl;
x *= (l2-l1)/g;
y *= (l2-l1)/g;
// cout<<x<<" "<<y<<endl;
g = abs(g);
ll t1 = w1/g, t2 = w2/g;
if(x > 0 && y > 0)
{
if(y/t1 > x/t2)
{
y -= x/t2*t1;
x -= x/t2*t2;
}
else
{
x -= y/t1*t2;
y -= y/y1*t1;
}
}
else if(x < 0 && y >= 0)
{
x += -x/t2*t2;
y += -x/t2*t1;
if(x < 0)
{
x += t2;
y += t1;
}
}
else if(x >= 0 && y < 0)
{
x += -y/t1*t2;
y += -y/t1*t1;
if(y < 0)
{
x += t2;
y += t1;
}
}
else if(x < 0 && y < 0)
{
if(-x/t2 > -y/t2)
{
x += -x/t2*t2;
y += -x/t2*t1;
}
else
{
x += -y/t1*t2;
y += -y/t1*t1;
}
if(x < 0 || y < 0)
{
x += t2;
y += t1;
}
}
printf("%I64d\n",l1+x*w1);
return 0;
}