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.
![](https://i-blog.csdnimg.cn/blog_migrate/fe53d5a0a13bcd430d14047ba104eba6.png)
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.
5 4 2 1 1 0 1 2 3
3
1023 1 2 1 0 1 2 1 1
-1
给定5个值 m h1,a1,x1,y1,h2,a2,x2,y2
两个式子
每次变化h1 h2同时变化 问最少经过几次变化可以使得h1=a1 h2=a2同时成立
一开始的想法是扩展欧几里德 想了一段时间 没有列出感觉正确的式子
然后就有了一个想法 因为要模固定的m值 肯定会出现循环结 如果a1 a2不在相应的循环中 就不会得到这两个值
如果在循环中就要求出相应的位置 使得这个位置上的值分别为a1 a2 这就跟a1 a2何时第一次出现在循环中有关
我就按着这个思路去写了 因为不清楚循环结的长度 就一直枚举i 结果tle
结束之后 发现枚举m次就可以了 因为模m的情况最多就是只有m种 所以可以先枚举m次 得到a1 a2在循环中第一次出现的位置 然后再枚举m次 就可以得到循环结的长度了
另外 还要注意 a1 a2可能只在循环中出现一次 因为模m可能会得到0 如果此时y=0 则后面的值会一直是0
还有 数据会爆int
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
int main()
{
// fread;
ll m;
while(scanf("%I64d",&m)!=EOF)
{
ll h1,a1,x1,y1,h2,a2,x2,y2;
scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d",&h1,&a1,&x1,&y1,&h2,&a2,&x2,&y2);
ll xun1=0,xun2=0,num1=0,num2=0;
ll h;
h=h1;
// cout<<"h11111111"<<h<<" "<<a1<<endl;
for(ll i=1;i<=2*m;i++)
{
h=(x1*h+y1)%m;
// cout<<"h1111111"<<h<<endl;
if(h==a1)
{
if(num1==0)
{
num1=i;
}
else if(xun1==0)
{
xun1=i-num1;
break;
}
}
}
h=h2;
for(ll i=1;i<=2*m;i++)
{
h=(x2*h+y2)%m;
// cout<<"ii "<<i<<"hh "<<h<<endl;
if(h==a2)
{
if(num2==0)
{
num2=i;
}
else if(xun2==0)
{
xun2=i-num2;
// cout<<"i "<<i<<endl;
}
}
}
// cout<<"num "<<num1<<" "<<num2<<endl;
// cout<<"xun "<<xun1<<" "<<xun2<<endl;
if(num1==0||num2==0)
{
puts("-1");
continue;
}
else
{
ll flag=0;
for(ll i=0;i<=m;i++)
{
if(xun2==0)//a2在序列中只会出现一次
{
if(num1+i*xun1==num2)
{
printf("%I64d\n",num1+i*xun1);
flag=1;
break;
}
}
else
{
if(num1+i*xun1>=num2&&(num1+i*xun1-num2)%xun2==0)
{
printf("%I64d\n",num1+i*xun1);
flag=1;
break;
}
}
}
if(!flag)
puts("-1");
}
}
return 0;
}