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

本文解析了CodeForces上E题Vanya and Field的解题思路及实现代码。该题要求在一个n*n的网格中寻找最优起始点以最大化经过的苹果树数量。文章详细介绍了如何通过寻找循环路径并利用坐标转换来解决问题。

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\n",ans1,ans2);

}

 

转载于:https://www.cnblogs.com/qscqesze/p/4970538.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值