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)

题目给出  gcd( n , dx ) = gcd( n , dy ) = 1 , 意味着(0~n-1)每个数都可以遍历到。

然后从x,y轴方向0坐标开始分别预处理出的n个数。

对于两颗苹果树,如果他们 y坐标到0坐标的距离 与 x坐标到0坐标的距离 两者的差值相同。

那么这两个坐标是来自同一个环的。

扫一遍m个点以后取出最大的就可以了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <algorithm> using namespace std;
typedef long long LL;
typedef pair<LL,int> pii;
const int N = ;
const int M = ;
const int inf = 1e9+;
#define X first
#define Y second int n , m , dx , dy , x[N] , y[N] , cnt[N] , id ;
vector<pii>e;
void test() {
for( int i = ; i < n ; ++i ) cout << x[i] << ' ' ; cout <<endl ;
for( int i = ; i < n ; ++i ) cout << y[i] << ' ' ; cout <<endl ;
}
void Run() { memset( cnt , , sizeof cnt ) ;
x[] = ; id = ; for( int i = dx ; i != ; i = (i+dx)%n ) x[i] = id++;
y[] = ; id = ; for( int i = dy ; i != ; i = (i+dy)%n ) y[i] = id++;
// test();
e.resize(m);
for( int i = ; i < m ; ++i ) {
cin >> e[i].X >> e[i].Y ;
int disx = ( x[e[i].X] + n ) % n ;
int disy = ( y[e[i].Y] + n ) % n ;
cnt[ (disx + n - disy)%n ] ++ ;
}
LL ans = ;
for( int i = ; i < n ; ++i ) {
if( cnt[ans] < cnt[i] ) ans = i;
}
cout << ( ans*dx ) % n << ""<< endl ;
}
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
ios::sync_with_stdio(false);
while( cin >> n >> m >> dx >> dy ) Run();
}

Codeforces 492E Vanya and Field的更多相关文章

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

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

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

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

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

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

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

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

  6. Vanya and Field

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

  7. cf492E Vanya and Field

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

  8. Codeforces 677D Vanya and Treasure 暴力+BFS

    链接 Codeforces 677D Vanya and Treasure 题意 n*m中有p个type,经过了任意一个 type=i 的各自才能打开 type=i+1 的钥匙,最初有type=1的钥 ...

  9. 【cf492】E. Vanya and Field(拓展欧几里得)

    http://codeforces.com/contest/492/problem/E 一开始没时间想,,诶真是.. 挺水的一道题.. 将每个点的横坐标都转换成0,然后找纵坐标有多少即可..即解方程 ...

随机推荐

  1. vue 移动端列表筛选功能实现

    最近兴趣所致,打算使用vant搭建一个webapp,由于需要使用列表筛选,没有找到合适组件,于是写了一个简单的功能,权当记录. 效果如下:        HTML: <div class=&qu ...

  2. JS的组成和变量

    JavaScript中的变量和数据类型 Js做客户端语言 按照相关的Js语法,去操作页面中的元素,有时还要操作浏览器里面的一些功能 Js由三部分组成: ECMAScript(ES):描述了该语言的语法 ...

  3. 20180209-os模块

    下面将学习关于os模块的相关操作 项目练习的目录结构如下:所有的操作都是基于os_exercise.py模块 1.获取当前的Python脚本的工作目录路径 os.getcwd() # 1.获取当前目录 ...

  4. MySQL数据库使用xtrabackup备份实现小例子

    关于MySQL数据库的备份的工具和方式也比较多,本文只简单介绍一些我司一个平台的备份方案.Xtrabackup是由percona开源的免费数据库热备份软件,但是只能对InnoDB数据库和XtraDB存 ...

  5. excel条件格式 满足包含xx的整行高亮

    条件格式-->新建规则-->使用公式确定要设置格式的单元格   =COUNTIF($D4,"*_S_*")   =COUNTIF($D4,"*_M_*&quo ...

  6. 命令行启动appium服务

    Android终端 appium --avd test -a 127.0.0.1 -p 4723 --language "zh_CN" --locale "CN" ...

  7. InputStream接口的常见实现类

    一. FileInputStream FileInputStream可以从系统文件中获取输入字节,也从可以从诸从图象数据的的原始字节流中读取. 如果是读取字符串流,推荐使用FileReader. 感觉 ...

  8. hdu 1451 Area in Triangle(计算几何 三角形)

    Given a triangle field and a rope of a certain length (Figure-1), you are required to use the rope t ...

  9. 数字滚动动画效果 vue组件化

    参考了这篇文章,作者思路很清晰,简单做了下修改,蟹蟹作者,链接在此:https://www.jb51.net/css/685357.html#comments 主要思路是利用css属性writing- ...

  10. angularjs radio 默认选中

    添加ng-model后checked="checked"失效,可见angularjs也不好,会失效html标准属性 解决:添加ng-checked="1" &l ...