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+ ...
随机推荐
- webpack坑系列--安装webpack-cli
综下所述: 还是装3.11.0版本吧 cnpm i webpack@3.11.0 -D 最近在学习webpack过程中,根据官方文档推荐局部安装webpack,执行webpack -h命令出现以下提示 ...
- 【SQL Server 优化性能的几个方面】(转)
转自:http://blog.csdn.net/feixianxxx/article/details/5524819 SQL Server 优化性能的几个方面 (一).数据库的设计 可以参看最 ...
- js中去掉字符中间空格和首尾空格
转载: https://www.jb51.net/article/109522.htm 1. 去掉字符串前后所有空格: 代码如下: ? 1 2 3 4 function Trim(str) { ...
- asp.net web 开发中配置web.config
一.配置数据库连接字符串 <connectionStrings> <add name="CaoLPractise" connectionString=" ...
- sed 处理一行,替换 删除
sed option 'action' filename (文件可多个) option: -i 直接在原文件中修改! -n 安静模式,默认情况所有数据都会被列出,但-n只有经过动作处理的那一行才被列出 ...
- Oracle11G的用户解锁、卸载以及基础操作
Oracle用户解锁 [以下操作,必须以超级管理员身份登录,才能修改]oracle安装后,会默认生成很多个用户 以超级管理员身份登录,请注意,其中的空格符:[ sys是一个超级管理员,有最大的权限,d ...
- UVALive 4262——Trip Planning——————【Tarjan 求强连通分量个数】
Road Networks Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Stat ...
- Ionic 解决gradle下载慢的问题
问题 使用Ioinc添加安卓平台或者编译的时候,提示gradle-XXX-all.zip下载,此进度缓慢. 解决 下载gradle对应的zip文件. 参考资源:http://services.grad ...
- client的使用
document.documentElement.clientHeight = 464 // 指窗口的可见高度的大小 document.body.clientHeight = 1577 // 指窗口的 ...
- 解决“SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT 'OpenRowset/OpenDatasource' 的访问……”【转】
SQL Server 阻止了对组件 /'Ad Hoc Distributed Queries/' 的访问 在Sql Server中查询一下Excel文件的时候出现问题: SELECT * FROM ...