「CF492E」Vanya and Field
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.
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.
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.
1 2 3 4 5 6 |
5 5 2 3 0 0 1 2 1 3 2 4 3 1 |
1 |
1 3 |
1 2 3 4 |
2 3 1 1 0 0 0 1 1 1 |
1 |
0 0 |
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)
题解
预处理f[i]表示x坐标移动i,y坐标的移动量 (mod n)
然后把所有坐标变成(0,p),按照p划分集合
输出最大集合的任一元素
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<map> #include<ctime> #include<vector> #include<set> #include<cmath> #include<algorithm> #define inf 1000000000 #define ll long long #define pa pair<int,int> using namespace std; int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int resx,resy; int n,m,dx,dy,ans; int x[100005],y[100005],d[1000005],q[1000005]; int main() { n=read();m=read();dx=read();dy=read(); for(ll i=1;i<n;i++) if(d[(i*dx)%n]==0)d[(i*dx)%n]=i; for(int i=1;i<=m;i++) { x[i]=read();y[i]=read(); ll t=d[n-x[i]%n]; t=(t*dy+y[i])%n; q[t]++; if(q[t]>ans)ans=q[t],resx=x[i],resy=y[i]; } printf("%d %d\n",resx,resy); return 0; } |