【cf492】E. Vanya and Field(拓展欧几里得)
http://codeforces.com/contest/492/problem/E
一开始没时间想,,诶真是。。
挺水的一道题。。
将每个点的横坐标都转换成0,然后找纵坐标有多少即可。。即解方程
$$a \times dx \equiv x(mod n)$$
然后注意开long long
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; } const int N=1e6+10;
void exgcd(ll a, ll b, ll &d, ll &x, ll &y) {
if(!b) { d=a; x=1; y=0; return; }
exgcd(b, a%b, d, y, x); y-=a/b*x;
}
int n, m, c[N];
ll x, y, d, a, b, dx, dy;
int main() {
cin >> n >> m >> dx >> dy;
for1(i, 1, m) {
cin >> x >> y;
if(x==0) { c[y]++; continue; }
exgcd(dx, n, d, a, b);
if(x%d!=0) continue;
a=(a+n)%n;
a=(a*x)%n;
x=((x-a*dx)%n+n)%n;
y=((y-a*dy)%n+n)%n;
c[y]++;
}
int mx=0, pos=-1;
rep(i, n) if(c[i]>mx) pos=i, mx=c[i];
//rep(i, n) dbg(c[i]);
if(pos==-1) cout << x << ' ' << y << endl;
else cout << 0 << ' ' << pos << endl;
return 0;
}
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)
【cf492】E. Vanya and Field(拓展欧几里得)的更多相关文章
- cf492E. Vanya and Field(扩展欧几里得)
题意 $n \times n$的网格,有$m$个苹果树,选择一个点出发,每次增加一个偏移量$(dx, dy)$,最大化经过的苹果树的数量 Sol 上面那个互素一开始没看见,然后就GG了 很显然,若$n ...
- NOIP2012拓展欧几里得
拉板题,,,不说话 我之前是不是说过数据结构很烦,,,我想收回,,,今天开始的数论还要恶心,一早上听得头都晕了 先来一发欧几里得拓展裸 #include <cstdio> void gcd ...
- poj 1061 青蛙的约会 拓展欧几里得模板
// poj 1061 青蛙的约会 拓展欧几里得模板 // 注意进行exgcd时,保证a,b是正数,最后的答案如果是负数,要加上一个膜 #include <cstdio> #include ...
- bzoj4517: [Sdoi2016]排列计数--数学+拓展欧几里得
这道题是数学题,由题目可知,m个稳定数的取法是Cnm 然后剩下n-m本书,由于编号为i的书不能放在i位置,因此其方法数应由错排公式决定,即D(n-m) 错排公式:D[i]=(i-1)*(D[i-1]+ ...
- POJ 2891 Strange Way to Express Integers(拓展欧几里得)
Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...
- POJ1061 青蛙的约会-拓展欧几里得
Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事 ...
- BZOJ-2242 计算器 快速幂+拓展欧几里得+BSGS(数论三合一)
污污污污 2242: [SDOI2011]计算器 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 2312 Solved: 917 [Submit][S ...
- BZOJ-1407 Savage 枚举+拓展欧几里得(+中国剩余定理??)
zky学长实力ACM赛制测试,和 大新闻(YveH) 和 华莱士(hjxcpg) 组队...2h 10T,开始 分工我搞A,大新闻B,华莱士C,于是开搞: 然而第一题巨鬼畜,想了40min发现似乎不可 ...
- poj2891 拓展欧几里得
//Accepted 164 KB 16 ms //拓展欧几里得 //m=a1*x+b1 --(1) //m=a2*(-y)+b2 --(2) //->a1*x+a2*y=b2-b1 //由欧几 ...
- [zoj 3774]Power of Fibonacci 数论(二次剩余 拓展欧几里得 等比数列求和)
Power of Fibonacci Time Limit: 5 Seconds Memory Limit: 65536 KB In mathematics, Fibonacci numbe ...
随机推荐
- Vagrant up Warning: Authentication failure. Retrying...
Vagrant up Warning: Authentication failure. Retrying... Vagrant up Warning: Authentication failure. ...
- CSS布局之脱离文档流详解——浮动、绝对定位脱离文档流的区别
1.代码 (1)示例代码1 <!DOCTYPE html> <html lang="zh"> <head> <meta charset=& ...
- sql存储过程等-版本控制
数据库开发人员总在想,每次修改了函数/存储过程,我们都得自己做备份,用以历史参考,当发现错误的时候,可以回滚 SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON ...
- 局域网Ubuntu与WinXP实现文件共享
时间:2008-11-28 11:27:55 从新立得软件包管理器中安装Samba和Smbfs.Samba是在Unix系统中用于共享文件和打印机的应用软件.Smbfs is a filesyste ...
- java基础学习总结——GUI编程(二) 未学习
一.事件监听
- python笔记-列表和元组
列表和元组: -可以将列表和元组当成普通的数组 -列表和元组可以保存任意类型的python对象 -通过从0开始的数字索引访问元素 -列表和元组可以存储不同类型的对象 列表和元组的区别: -列表元素使用 ...
- 线程池c3p0和dbcp2的配置初始化实例
一.c3p0 public class ConnectionManager { public static ComboPooledDataSource dataSource; static { try ...
- sql server数据库查询超时报错
报错信息如下: 链接服务器"DBJointFrame"的 OLE DB 访问接口 "SQLNCLI10" 返回了消息 "查询超时已过期". ...
- jquery常见插件用法表
一:美化select表单:chosen.jquery.js http://harvesthq.github.io/chosen/ 关于ajax更新列表后需要触发下插件的事件,才会表现出来:(http: ...
- Docker背景介绍
背景介绍 Docker是PasS提供商DoctCloud开源的一个基于LXC的高级容器引擎,源代码托管在Github上,基于go语言并遵从Apache2.0协议开源.Docker近期非常火热,无论是从 ...