CodeForces 492E Vanya and Field (思维题)
2 seconds
256 megabytes
standard input
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.
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.
5 5 2 3
0 0
1 2
1 3
2 4
3 1
1 3
2 3 1 1
0 0
0 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)
好困╯﹏╰,不填坑了,睡觉去。有需要问思路的留言
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1e9;
const double eps = 1e-;
const int N = ;
int cas = ; struct _node{
int x,y,k;
void set(int _k, int _x, int _y)
{
k = _k;
x = _x;
y = _y;
}
};
_node point[N];
int xk[N],cnt[N];
int n,m,dx,dy; void run()
{
point[].set(,,);
xk[] = ;
for(int i=; i<n; i++)
{
point[i].set(i,(point[i-].x+dx)%n,(point[i-].y+dy)%n);
xk[point[i].x] = i;
}
memset(cnt,,sizeof(cnt));
int x,y,k,y0,yk;
while(m--)
{
scanf("%d%d",&x,&y);
k = xk[x];
yk = point[k].y;
y0 = (y - yk + n) % n;
cnt[y0]++;
}
int mx=cnt[], pos=;
for(int i=;i<n;i++)
if(mx < cnt[i])
mx=cnt[i], pos=i;
printf("0 %d\n",pos);
} int main()
{
#ifdef LOCAL
freopen("case.txt","r",stdin);
#endif
while(scanf("%d%d%d%d",&n,&m,&dx,&dy)!=EOF)
run();
return ;
}
思路:
题目的关键条件是这个 
首先想个问题,先是一维的情况下,假设只有一行的格子,长度为x,每次能移动的距离为dx,而且gcd(x,dx)=1,这样手动模拟一下,可以发现规律,从某个点出发直到回到这个点上,步数均为x次,而且每次落下的点都是不重复的,也即这x次的位置覆盖了整条方格上的每一个方格。
那现在二维的情况下,gcd(n,dx) = gcd(n,dy) = 1,也就是从某一行和某一列的交点出发,要重新回到这个交点,就要经过n步而且这n步覆盖了每一行每一列。每个循环必须每个x走一次以及每个y走一次,n个格子属于一组循环里面的,总共有n*n个格子,所以有n组循环。一组循环内的格子是等价的。同一行内的n个格子均来自不同的n组。
现在考虑一组特殊的循环,这组循环从(0,0)开始出发
走了第一步以后就到 (dx%n, dy%n)
第二步到 (2*dx%n, 2*dy%n)
第k步到 (k*dx%n, k*dy%n)
用一个对应关系存储,从(0,0)出发的,经过了k步以后,会到达位置(x[k] , y[k])。
然后考虑普遍的情况了,每组循环都比如经过(0, y0)这个点
从这个点出发的第一步 (dx%n, (y0+dy)%n)
第k步到 (dx%n, (y0+dy)%n), 也即(x[k], (y0+y[k])%n)
那么现在给你某个坐标(x,y),要怎么算出他属于哪一组循环的呢
根据等式x[k]==x,可以求出对应的k的值
那就能求出对应的y[k]了,然后y0+y[k]==y → y0=y-y[k]
这样就知道这个(x,y) 是属于 (0,y0)这一组的了
那么算法大概是这样:
预处理出x[k],y[k],时间复杂度o(n)
遍历每一个apple的坐标(x,y),求出对应的坐标(0,y0),然后cnt[y0]++,复杂度o(m)
找出值最大的cnt[y],答案就是(0,y)了。 总复杂度o(n+m)
代码如上。
CodeForces 492E Vanya and Field (思维题)的更多相关文章
- codeforces 492E. Vanya and Field(exgcd求逆元)
题目链接:codeforces 492e vanya and field 留个扩展gcd求逆元的板子. 设i,j为每颗苹果树的位置,因为gcd(n,dx) = 1,gcd(n,dy) = 1,所以当走 ...
- 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 ...
- Codeforces 492E Vanya and Field
E. Vanya and Field time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #280 (Div. 2)E Vanya and Field(简单题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 本场题目都比较简单,故只写了E题. E. Vanya and Field Vany ...
- C. Nice Garland Codeforces Round #535 (Div. 3) 思维题
C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- Codeforces 515C 题解(贪心+数论)(思维题)
题面 传送门:http://codeforces.com/problemset/problem/515/C Drazil is playing a math game with Varda. Let’ ...
- Codeforces 1188B - Count Pairs(思维题)
Codeforces 题面传送门 & 洛谷题面传送门 虽说是一个 D1B,但还是想了我足足 20min,所以还是写篇题解罢( 首先注意到这个式子里涉及两个参数,如果我们选择固定一个并动态维护另 ...
- Codeforces 1365G - Secure Password(思维题)
Codeforces 题面传送门 & 洛谷题面传送门 首先考虑一个询问 \(20\) 次的方案,考虑每一位,一遍询问求出下标的这一位上为 \(0\) 的位置上值的 bitwise or,再一遍 ...
- Codeforces 1129E - Legendary Tree(思维题)
Codeforces 题面传送门 & 洛谷题面传送门 考虑以 \(1\) 为根,记 \(siz_i\) 为 \(i\) 子树的大小,那么可以通过询问 \(S=\{2,3,\cdots,n\}, ...
随机推荐
- dsp2812 pwm配置
肚子疼了好几天,今天稍微好点,简单写点东西. 好几个月前做的项目,有些地方已经记不清楚了,但是突然客户又来问关于代码配置的情况,重新查看了代码,把相关的知识也整理一下. dsp2812中有好几个时钟相 ...
- centos web+mysql服务器的安全
今天闲来无事,拿来X-Scan-v3.3 来扫描自己的服务器,开放端口有22,80,443,3306:3306端口被扫出来,呵呵,那可不得了: 一,屏蔽扫描器扫出3306端口,因为web和数据库是在同 ...
- 简化Hadoop命令
1. 安装客户端(通过端用户可以方便的和集群交互) 2. 简化Hadoop命令 修改~/.bashrcalias hadoop='/home/work/hadoop/client/hadoop-cli ...
- JAVAScript中DOM与BOM的差异分析
JAVAScript 有三部分构成,ECMAScript,DOM和BOM,根据浏览器的不同,具体的表现形式也不尽相同.我们今天来谈一谈DOM和BOM这俩者之间的差异. 用百科上的来说: 1. DOM是 ...
- 大话设计模式--建造者模式 Builder -- C++实现实例
1. 建造者模式,将一个复杂对象的构建与它的表示分离, 使得同样的构建过程可以创建不同的表示. 用户只需要指定需要建造的类型就可以得到他们,而具体建造的过程和细节就不需要知道了. 关键类Directo ...
- linux用户管理与用户组的重要文件
用户管理的2个重要文件:/etc/passwd和/etc/shadow. /etc/passwd文件里存放的是用户的信息,其中不包含密码:passwd文件中每一行代表一个用户,且每一行分为7个字段使用 ...
- php简单实现通讯录采集,我的第一个php,适合新手
起源于要整理通讯录,原先用的是文件调查,现在学了php,就自己试一下.程序短小精悍,适于学习.有两个文件,bj.html用于显示和采集表单信息.bj.php用于处理数据和反馈结果.突出之处在于可以上传 ...
- getline()函数详解 (2013-03-26 17:19:58)
学习C++的同学可能都会遇到一个getline()函数,譬如在C++premer中,标准string类型第二小节就是“用getline读取整行文本”.书上给的程序如下: int main() { ...
- jQuery对象和DOM对象的相互转化实现代码
jQuery对象和DOM对象相互转化 jQuery对象和DOM对象 jQuery对象就是通过jQuery包装DOM对象后产生的对象.jQuery对象是jQuery独有的,其可以使用jQuery里的方法 ...
- Opencv - Android 配置安装
1.道具们: windows 7 64位 OpenCV-2.4.6-android-sdk-r2 ( http://sourceforge.net/projects/opencvlibrary/fil ...