codeforces 492E. Vanya and Field(exgcd求逆元)

时间:2023-03-09 14:31:05
codeforces 492E. Vanya and Field(exgcd求逆元)
题目链接:codeforces 492e vanya and field

留个扩展gcd求逆元的板子。

设i,j为每颗苹果树的位置,因为gcd(n,dx) = 1,gcd(n,dy) = 1,所以当走了n步后,x从0~n-1,y从0~n-1都访问过,但x,y不相同.

所以,x肯定要经过0点,所以我只需要求y点就可以了。

i,j为每颗苹果树的位置,设在经过了a步后,i到达了0,j到达了M.

则有

1----------------------(i + b * dx) % n = 0

2-----------------------(j + b * dy) % n = M % n

则2 * dx - 1 * dy消掉未知数 b.

得方程。           codeforces 492E. Vanya and Field(exgcd求逆元)

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const ll mod = 1e9+;
const int maxn = 1e6+;
int vis[maxn];
ll n,m,dx,dy;
ll ex_gcd(ll a,ll b, ll &x, ll &y)
{
if (b == )
{
x = ;
y = ;
return a;
}
else
{
ll gcd = ex_gcd(b, a % b, x, y);
ll t = x;
x = y;
y = t - (a / b) * y;
return gcd;
}
}
int main()
{
cin>>n>>m>>dx>>dy;
ll x,y;
ex_gcd(dx,n,x,y);
while(x<) x+=n;
ll inv = x;
ll ans = ;
ll i,j;
ll mm;
for(int k=;k<m;k++)
{
scanf("%I64d %I64d",&i,&j);
mm = ((dx*j-dy*i)%n+n)%n*inv%n;
vis[mm]++;
}
ll mmm = ;
for(int k=;k<n;k++)
{
if(vis[k]>ans)
{
ans = vis[k];
mmm = k;
}
}
printf("0 %I64d\n",mmm);
return ;
}