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.

Sample test(s)
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)

这道真是想法题了……

首先,从一个点出发一直走,它能到达的点的集合一定是一个环(因为要是能一直走到当前没走过的点那还得了)

然后要考虑这样的集合有什么性质

把第一个样例拿来模拟一下,不取模看的更清楚:

(1,3)

(3,6)

(5,9)

(7,12)

(9,15)

(11,18)

这样规律就很清楚了吧:对于n*n的格子,从一个点出发走了n步之后回到这个点,集合里一定有n个元素

所以有n个集合,用(0,0)到(0,n-1)共n个在不同集合的点标记它们

然后从(0,0)开始枚举往上走,就可以算出每一行中哪一个格子是和(0,0)在同一集合

那么其他点依次右移一格就知道在那一个集合里了

最后每一个集合里的点都是可以相互到达的,随便输出一个就好了

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define pi 3.1415926535897932384626433832795028841971
#define mod 1000007
using namespace std;
inline LL read()
{
LL 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;
}
struct po{
int x,y,rnk;
}point[1000010];
int head[1000010];
int n,m,xx,yy,cnt,ans,mx;
int nx,ny;
int floor[1000010];
int sum[1000010];
int main()
{
n=read();m=read();xx=read();yy=read();
for(int i=1;i<=n+1;i++)
{
nx+=xx;ny+=yy;
if (nx>=n)nx-=n;
if (ny>=n)ny-=n;
floor[nx]=ny;
}
for (int i=1;i<=m;i++)
{
int nx=read();
int ny=read();
int flo=floor[nx];
int dire=ny-flo;if (dire<0)dire+=n;
sum[dire]++;
if(sum[dire]>mx)
{
mx=sum[dire];
ans=dire;
} }
printf("%d %d\n",0,ans);
return 0;
}

cf492E Vanya and Field的更多相关文章

  1. cf492E. Vanya and Field(扩展欧几里得)

    题意 $n \times n$的网格,有$m$个苹果树,选择一个点出发,每次增加一个偏移量$(dx, dy)$,最大化经过的苹果树的数量 Sol 上面那个互素一开始没看见,然后就GG了 很显然,若$n ...

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

    E. Vanya and Field Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/492/pr ...

  3. Codeforces Round #280 (Div. 2)E Vanya and Field(简单题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 本场题目都比较简单,故只写了E题. E. Vanya and Field Vany ...

  4. Vanya and Field

    Vanya and Field 题目链接:http://www.codeforces.com/problemset/problem/492/E 逆元 刚看到这题的时候一脸懵逼不知道从哪下手好,于是打表 ...

  5. codeforces 492E. Vanya and Field(exgcd求逆元)

    题目链接:codeforces 492e vanya and field 留个扩展gcd求逆元的板子. 设i,j为每颗苹果树的位置,因为gcd(n,dx) = 1,gcd(n,dy) = 1,所以当走 ...

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

  7. CodeForces 492E Vanya and Field (思维题)

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

  8. Codeforces 492E Vanya and Field

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

  9. 【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 a ...

随机推荐

  1. CentOS 设置mysql的远程访问

    好记性不如烂笔头,记录一下. 安装了MySQL默认是拒绝远程连接的. 首先进入数据库,使用系统数据库mysql. mysql -u root -p mysql #回车,然后输入则使用了系统数据库 接着 ...

  2. 设置MyEclipse的右击新建后面的选项

    转自:http://blog.sina.com.cn/s/blog_929b09ac010197g6.html,做了修改 最近发现在MyEclipse右击后,新建后面的菜单选项太少了,新建一个文件,还 ...

  3. socket实例2

    第二个实例创建一个java工程,基于tomcat服务器,程序运行时会启动客户端,实现了一个客户端向其他的客户端发送即时信息的功能 MainWindow.java package com.jikexue ...

  4. Linux网络配置相关

    路由相关 #添加到主机的路由 route add -host 192.168.1.2 dev eth0 route add -host 192.168.1.2 gw 192.168.1.1 注1:添加 ...

  5. cocos2d-x anchorPoint

    之前一直没有用过anchorPoint,也感觉用这个东西的地方相对比较少的,都是直接使用世界坐标来定位的. 但是,在现在这个项目中,却有同事使用了这个anchorPoint,使用是使用了,但是,在碰撞 ...

  6. 使用const 提高函数的健壮性

    1.如果输入参数采用“指针传递”,那么加const 修饰可以防止意外地改动该指针指向的内存单元,起到保护的作用. 例如:void StringCopy(char *strDest, const cha ...

  7. DataGrid( 数据表格) 组件[8]

    本节课重点了解 EasyUI 中 DataGrid(数据表格)组件的使用方法,这个组件依赖于Panel(面板).Resizeable(调整大小).LinkButton(按钮).Pageination( ...

  8. MVC Controller 基类中的Request

    今天在测试自己MVC程序的时候发现之前写代码的一个BUG,需求是每个页面要获取当前URL链接中包含的城市ID,我把获取url的方法写到了Controller的基类BaseController(Base ...

  9. JavaScript“闭包”精解

    一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域无非就是两种:全局变量和局部变量. 详细了解 Javascript语言的特殊之处,就在于函数内部可以直接读 ...

  10. hdu 1076

    水题 AC代码: #include <iostream> using namespace std; int main() { int i,k,t,y,n; cin>>t; wh ...