[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.

样例输入 SampleInput

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

样例输出 SampleOutput

3 4


Codeforces 492E


代码 Code

因为 dx 和 dy 都与 n 互质可以发现每个点走一圈之后一定会回到这个点。所以预处理出横坐标移动 x 格所需要的步数。然后可以将每个苹果树的坐标通过坐标变换移动到 (0,x) 位置上,因此记录每个 (0,x) 能有多少个苹果树移动到这个位置,最多的点即为答案。

#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;
}