E. Vanya and Field
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/492/problem/E
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 . 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.
Sample Input
5 5 2 3
0 0
1 2
1 3
2 4
3 1
Sample Output
1 3
HINT
题意
有一个n*n的方格,有m棵苹果树
一开始假设在(x,y),那么下一次可以在(x+dx,y+dy)位置,可以无限走
然后问你起点定在什么位置,可以如果最多的苹果树
题解:
题目说了,gcd(n,dx)=0,那么很显然,这个集合里面有n个元素(这个可以用exgcd证明
那么也很容易证明,每一行的每个元素都属于不同的集合
然后我们就可以先暴力找到一个集合,然后这个苹果树和这个集合偏移多少,那么这个苹果树就属于第几个集合
代码
#include<iostream> #include<stdio.h> using namespace std; #define maxn 1000006 int X[maxn]; int ans[maxn]; int main() { int n,m,dx,dy; scanf("%d%d%d%d",&n,&m,&dx,&dy); int xx=0,yy=0; for(int i=0;i<n;i++) { X[xx]=yy; xx+=dx; yy+=dy; if(xx>=n)xx-=n; if(yy>=n)yy-=n; } int ans1=0,ans2=0,ans3=0; for(int i=0;i<m;i++) { int x,y;scanf("%d%d",&x,&y); int tmp = y-X[x];if(tmp<0)tmp+=n; ans[tmp]++; if(ans[tmp]>=ans3) { ans3=ans[tmp]; ans1=0,ans2=tmp; } } printf("%d %d ",ans1,ans2); }