【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 ...
随机推荐
- 数据库case,when学习
前几天工作中遇到了一个数据库统计相关的东西,主要使用case,when实现.如今说说基本情况: 有两个表school,studens,当中 school表结构例如以下: students表结构例如以下 ...
- Java网络编程-HTTP协议
HTTP协议的定义 这篇文章暂时不研究HTTP底层的TCP/IP的握手和挥手过程,只从表面的交互流程分析HTTP协议. HTTP英文全称是Hypertext Transfer Protpcol,也就是 ...
- 百度地图总结第二篇--POI检索功能
简单介绍: 眼下百度地图SDK所集成的检索服务包含:POI检索.公交信息查询.线路规划.地理编码.行政区边界数据检索.在线建议查询.短串分享(包含POI搜索结果分享.驾车/公交/骑行/步行路线规划分享 ...
- Thrift实现C#调用Java开发步骤详解
概述 Thrift实现C#调用Java开发步骤详解 详细 代码下载:http://www.demodashi.com/demo/10946.html Apache Thrift 是 Facebook ...
- Android系统示例之ActionBarCompat
导入工程ActionBarCompat时,出现错误.从其他工程下拷贝project.propertiest文件过来,问题仍在.拷贝后需要重启Eclipse才解决.问题如下: [2013-07-03 1 ...
- Linux下将/TMP和/Var移动到共享分区
2007-03-09 03:25:08 整理数据 首先,必须创建一个新分区专门用于存储频繁修改的文件.您可能希望将这个分区置于单独的磁盘上以增强性能.接下来,我将逐步说明将 /tmp 和 /va ...
- Sphinx高亮显示关键字
选取程序中使用的一部分代码: public function buildExcerptRows($ids) { $options = array( 'before_match' => '< ...
- window.open()函数
http://hi.baidu.com/gagahjt/blog/item/7b76e0dee61b20aecd11661c.html open函数详解: window.open("sUrl ...
- 推荐10款最常用的Android开发工具
我们使用各种语言进行开发时,总是会用到各种各样的开发工具.有些开发工具是开发人员的必备品,有些则是为了提高开发效率而用.Android开发同样也会用到多种开发工具,供开发人员设计.创建.测试和发布程序 ...
- Linux命令-压缩解压命令:bzip2、bunzip2
bzip2是gzip的升级版 bzip2 [选项] 源文件名(压缩前) -k 保留源文件,(区别gzip不支持保留源文件) bunzip2 [选项] 源文件名(压缩后) 压缩文件: bzip2 -k ...