# [Codeforces492E] Vanya and Field

## 描述 Description

Vanya decided to walk in the field of size n × n cells. The field contains m apple trees, the i-th apple tree is at the cell with coordinates (xi, yi). Vanya moves towards vector (dx, dy). That means that if Vanya is now at the cell (x, y), then in a second he will be at cell ( ((x+dx)  mod  n,(y+dy)  mod  n) ). The following condition is satisfied for the vector: ( gcd(n,dx)=gcd(n,dy)=1 ), where ( gcd(a,b) )is the largest integer that divides both a and b. Vanya ends his path when he reaches the square he has already visited.

Vanya wonders, from what square of the field he should start his path to see as many apple trees as possible.

## 输入格式 InputFormat

The first line contains integers n, m, dx, dy(1 ≤ n ≤ 106, 1 ≤ m ≤ 105, 1 ≤ dx, dy ≤ n) — the size of the field, the number of apple trees and the vector of Vanya’s movement. Next m lines contain integers xi, yi (0 ≤ xi, yi ≤ n - 1) — the coordinates of apples. One cell may contain multiple apple trees.

## 输出格式 OutputFormat

Print two space-separated numbers — the coordinates of the cell from which you should start your path. If there are several answers you are allowed to print any of them.

6 6 1 1
0 0
1 1
2 1
0 1
1 2
3 4

3 4

Codeforces 492E

## 代码 Code

``````#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define ll long long
const ll maxn=1000005;
ll i,j,t,n,m,l,r,k,z,y,x;
ll a[maxn],f[maxn];
ll dx,dy,ansx,ansy,ans;
int main()
{
scanf("%I64d%I64d%I64d%I64d",&n,&m,&dx,&dy);
for (i=1;i<n;i++) a[(i*dx)%n]=i;
ans=0;
for (i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
t=(a[n-x]*dy+y)%n;
f[t]++;
if (f[t]>ans)
{
ans=f[t];
ansx=x;ansy=y;
}
}
printf("%I64d %I64d\n",ansx,ansy);
return 0;
}``````