Codeforces Round #280 (Div. 2) E. Vanya and Field 数学

E. Vanya and Field
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 . The following condition is satisfied for the vector: , where  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.

Input

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.

Output

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.

Examples
input
5 5 2 3
0 0
1 2
1 3
2 4
3 1
output
1 3
input
2 3 1 1
0 0
0 1
1 1
output
0 0
Note

In the first sample Vanya's path will look like: (1, 3) - (3, 1) - (0, 4) - (2, 2) - (4, 0) - (1, 3)

In the second sample: (0, 0) - (1, 1) - (0, 0)

题意:给你n*n的方块,m个苹果树的位置,每次可以从点(x,y)-> (x+dx,y+dy)的点,找到最长的那条链,里面包含最多的苹果树;

思路:因为gcd(x,dx)==1;

   首先在一维平面上,K*dx(mod n)==(0-(n-1));可以得到结论k的值为(0-n-1)的集合;

   反证法:如果有两点相等;

    扩展欧几里德:k*dx+y*n==z;

   特解k0显然在(0-n-1)范围内,k=k0+t*n/gcd(dx,n)=k0+t*n;k显然只有一个值在(0,n-1)内,所以一个值对应一个解;

   二维显然也是一样;

   从(0,0)暴力到(n-1,?);

   计算偏移量,得到答案,复杂度o(n);

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=2e5+10,M=1e6+10,inf=1e9+10,mod=1e9+7;
void exgcd(ll a, ll b, ll &d, ll &x, ll &y) {
    if(!b) { d=a; x=1; y=0; return; }
    exgcd(b, a%b, d, y, x); y-=a/b*x;
}
int flag[M];
map<int,int>ans;
int main()
{
    int n,m,dx,dy;
    scanf("%d%d%d%d",&n,&m,&dx,&dy);
    int st=0;
    int en=0;
    for(int i=0;i<n;i++)
    {
        st+=dx;
        en+=dy;
        st%=n;
        en%=n;
        flag[st]=en;
    }
    int ansx,ansy,maxx=0;
    for(int i=1;i<=m;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        int v=((y-flag[x])%n+n)%n;
        ans[v]++;
        if(ans[v]>maxx)
        {
            maxx=ans[v];
            ansx=x;
            ansy=y;
        }
    }
    printf("%d %d
",ansx,ansy);
    return 0;
}
原文地址:https://www.cnblogs.com/jhz033/p/5923836.html