- 将题目中所给条件用同余方程表示,可得 (s-1equiv pm a,s+l-1equiv pm bmod k).
- 于是可得 (lequiv pm apm b mod k),将四个 (l) 取模后记为 (l_1,l_2,l_3,l_4).
- 对于每个 (l) ,它一定是 (l_i+qcdot k,qin mathbb{N_+}) 的形式,它对应的答案为 (frac {ncdot k} {gcd(l_i+qcdot k,nk)}).
- 注意到 (gcd(l_i+qcdot k,nk)=gcd(nk,l_i\%k+qk\%nk)=gcd(nk,l_i+(q\%n)k)).所以在 (0sim n-1) 内枚举 (q) 即可.时间复杂度为 (O(n)).
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read()
{
ll out=0,fh=1;
char jp=getchar();
while ((jp>'9'||jp<'0')&&jp!='-')
jp=getchar();
if (jp=='-')
fh=-1,jp=getchar();
while (jp>='0'&&jp<='9')
out=out*10+jp-'0',jp=getchar();
return out*fh;
}
ll n,k,a,b;
ll gcd(ll A,ll B)
{
return B?gcd(B,A%B):A;
}
ll p[4];
int main()
{
n=read(),k=read(),a=read(),b=read();
p[0]=(k+a+b)%k;
p[1]=(k+a-b)%k;
p[2]=(k-a-b)%k;
p[3]=(k-a+b)%k;
ll maxans=0,minans=1e18;
for(int i=0; i<4; ++i)
{
for(ll q=0; q<=n-1; ++q)
{
ll ans=n*k/gcd(p[i]+q*k,n*k);
maxans=max(maxans,ans);
minans=min(minans,ans);
}
}
cout<<minans<<' '<<maxans<<endl;
return 0;
}