cf492E. Vanya and Field(扩展欧几里得)
题意
$n \times n$的网格,有$m$个苹果树,选择一个点出发,每次增加一个偏移量$(dx, dy)$,最大化经过的苹果树的数量

Sol
上面那个互素一开始没看见,然后就GG了
很显然,若$n$和$dx$互素的话,每个$x$都能到达
我们预处理出在每个点$x = 0$时的$y$,取一下最大值即可
求解需要用到扩展欧几里得
/* */
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<cmath>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/hash_policy.hpp>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define int long long
#define LL long long
#define ull unsigned long long
#define rg register
#define pt(x) printf("%d ", x);
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 22)], *p1 = buf, *p2 = buf;
//char obuf[1<<24], *O = obuf;
//void print(int x) {if(x > 9) print(x / 10); *O++ = x % 10 + '0';}
//#define OS *O++ = ' ';
using namespace std;
//using namespace __gnu_pbds;
const int MAXN = 1e6 + , INF = 1e9 + , mod = 1e9 + ;
const double eps = 1e-;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
} int N, M, dx, dy, a, b;
int num[MAXN];
int exgcd(int a, int b, int &x, int &y) {
if(!b) {x = ; y = ; return a;}
int r = exgcd(b, a % b, x, y);
int tmp = x; x = y; y = tmp - a / b * y;
return r;
}
main() {
N = read(); M = read(); dx = read(); dy = read();
int ans = ;
int x, y;
for(int i = ; i <= M; i++) {
x = read(), y = read();
if(x == ) {num[y]++; if(num[y] > num[ans]) ans = y;continue;}
int r = exgcd(dx, N, a, b);
a = (a + N) % mod;
a = (a * x) % N;
// a =
y = (y - a * dy % N + N) % N;
num[y]++;
if(num[y] > num[ans]) ans = y;
}
printf("%d %d", , ans); return ;
}
/* */
cf492E. Vanya and Field(扩展欧几里得)的更多相关文章
- Root(hdu5777+扩展欧几里得+原根)
Root Time Limit: 30000/1500 ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C.Ray Tracing (模拟或扩展欧几里得)
http://codeforces.com/contest/724/problem/C 题目大意: 在一个n*m的盒子里,从(0,0)射出一条每秒位移为(1,1)的射线,遵从反射定律,给出k个点,求射 ...
- UVA 12169 Disgruntled Judge 枚举+扩展欧几里得
题目大意:有3个整数 x[1], a, b 满足递推式x[i]=(a*x[i-1]+b)mod 10001.由这个递推式计算出了长度为2T的数列,现在要求输入x[1],x[3],......x[2T- ...
- UVA 10090 Marbles 扩展欧几里得
来源:http://www.cnblogs.com/zxhl/p/5106678.html 大致题意:给你n个球,给你两种盒子.第一种盒子每个盒子c1美元,可以恰好装n1个球:第二种盒子每个盒子c2元 ...
- POJ 1061 青蛙的约会 扩展欧几里得
扩展欧几里得模板套一下就A了,不过要注意刚好整除的时候,代码中有注释 #include <iostream> #include <cstdio> #include <cs ...
- 【64测试20161112】【Catalan数】【数论】【扩展欧几里得】【逆】
Problem: n个人(偶数)排队,排两行,每一行的身高依次递增,且第二行的人的身高大于对应的第一行的人,问有多少种方案.mod 1e9+9 Solution: 这道题由1,2,5,14 应该想到C ...
- poj 2891 扩展欧几里得迭代解同余方程组
Reference: http://www.cnblogs.com/ka200812/archive/2011/09/02/2164404.html 之前说过中国剩余定理传统解法的条件是m[i]两两互 ...
- poj 2142 扩展欧几里得解ax+by=c
原题实际上就是求方程a*x+b*y=d的一个特解,要求这个特解满足|x|+|y|最小 套模式+一点YY就行了 总结一下这类问题的解法: 对于方程ax+by=c 设tm=gcd(a,b) 先用扩展欧几里 ...
- poj 1061 扩展欧几里得解同余方程(求最小非负整数解)
题目可以转化成求关于t的同余方程的最小非负数解: x+m*t≡y+n*t (mod L) 该方程又可以转化成: k*L+(n-m)*t=x-y 利用扩展欧几里得可以解决这个问题: eg:对于方程ax+ ...
随机推荐
- vue中点击空白处隐藏弹框(用指令优雅地实现)
在写vue的项目的时候,弹框经常性出现,并要求点击弹框外面,关闭弹框,那么如何实现呢?且听我一一...不了,能实现效果就好 <template> <div> <div c ...
- lintcode-dfs实现二叉树的层序遍历
class Solution { /** * @param root: The root of binary tree. * @return: Level order a list of lists ...
- ReflectionUtil
import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang ...
- 转 安装EBS前期检查工具 - RDA - Health Check / Validation Engine Guide
http://blog.itpub.net/35489/viewspace-1295028/ 参考文档 RDA - Health Check / Validation Engine Gu ...
- Shell变量赋值语句不能有空格
a = 1是错的!!!!!只有 a=1才是正确的.
- TortoiseSVN 控制图标未显示或显示异常解决方法
- (转)centos7优化内核参数详解
centos7优化内核参数详解 原文:http://blog.csdn.net/xiegh2014/article/details/52132863 cat /etc/sysctl.conf #CTC ...
- python学习四(处理数据)
head first python中的一个数据处理的例子 有四个U10选手的600米成绩,请取出每个选手跑的最快的3个时间.以下是四位选手的9次成绩 James 2-34,3:21,2.34,2.45 ...
- HBase基础讲解
HBase定义 HBase 是一个高可靠.高性能.面向列.可伸缩的分布式存储系统,利用Hbase技术可在廉价PC Server上搭建 大规模结构化存储集群. HBase 是 ...
- 高可用数据同步方案-SqlServer迁移Mysql实战
简介 随着业务量的上升,以前的架构已经不满足业务的发展,数据作为业务中最重要的一环,需要有更好的架构作为支撑.目前我司有sql server转mysql的需求,所以结合当前业务,我挑选了阿里云开源的一 ...