【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 ...
随机推荐
- java.sql.SQLException: Data truncation: Truncated incorrect DOUBLE value
mysql 报这个异常:java.sql.SQLException: Data truncation: Truncated incorrect DOUBLE value update 表名 set c ...
- ScriptableObject 对象化的运用
http://www.cnblogs.com/oldman/articles/2409554.html using UnityEngine; using UnityEditor; using Syst ...
- Simple drag and drop
In computer graphical user interfaces, drag-and-drop is the action of (or support for the action of) ...
- Java lock 能被中断, synchronized 不能被中断
1.lock是可中断锁,而synchronized 不是可中断锁 线程A和B都要获取对象O的锁定,假设A获取了对象O锁,B将等待A释放对O的锁定, 如果使用 synchronized ,如果A不释放, ...
- Oracle Unicode转中文(解码)
Oracle Unicode转中文(解码) CreateTime--2018年3月29日15:23:30 Author:Marydon 情景描述: 将数据库中的某个字段误存储的是Unicode编码 ...
- post请求乱码
jsp页面中 <meta http-equiv="content-type" content="text/html; charset=UTF-8"& ...
- js特效,轻松实现内容的无缝平滑滚动
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http ...
- mybatis WARN No appenders could be found for logger的解决方法
log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).log4j:WARN ...
- 我的SpringMvc学习之路之注解
用注解取代配置文件可降低编程是不必要的麻烦和错误. @Controller 控制器定义 在一个class的上面写上@controller声明此类为控制器类.在配置中*.dispatcher.xml里 ...
- FPGA的EPCS 配置的2种方法 FPGA下载程序的方法(EPCS)
使用主动串行配置模式对Cyclone FPGA进行配置前,必须将配置文件写入串行配置器件EPCS.将配置文件写入EPCS的方法有三种: (1)在Quartus II的Programmer中,通过专门与 ...