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+ ...
随机推荐
- CF24D Broken robot 后效性DP
这题咕了好久..... 设$f[i][j]$表示从$(i,j)$到最后一行的期望步数: 则有 $ f[i][1]=\frac{1}{3}(f[i][1]+f[i][2]+f[i+1][1])+1$ $ ...
- 牛客网训练赛26D(xor)
题目链接:https://www.nowcoder.com/acm/contest/180/D 线性基的学习:https://www.cnblogs.com/vb4896/p/6149022.html ...
- Soup协议-即普通post请求,内容域xml
1.基础问题 1.1 soup-Simple Object Access Protocal简单对象访问协议 a).承载在http协议之上,http支持传输img/html/文件等,soup请求和响应域 ...
- SpringBoot | 第二十四章:日志管理之AOP统一日志
前言 上一章节,介绍了目前开发中常见的log4j2及logback日志框架的整合知识.在很多时候,我们在开发一个系统时,不管出于何种考虑,比如是审计要求,或者防抵赖,还是保留操作痕迹的角度,一般都会有 ...
- SpringBoot | 番外:使用小技巧合集
前言 最近工作比较忙,事情也比较多.加班回到家都十点多了,洗个澡就想睡觉了.所以为了不断更太多天,偷懒写个小技巧合集吧.之后有时间都会进行文章更新的.原创不易,码字不易,还希望大家多多支持!话不多说, ...
- rabbit的简单搭建,java使用rabbitmq queue的简单例子和一些坑
一 整合 由于本人的码云太多太乱了,于是决定一个一个的整合到一个springboot项目里面. 附上自己的项目地址https://github.com/247292980/spring-boot 以整 ...
- Elasticsearch支持的字段类型
es支持下列简单的字段类型: String: string Whole number: byte, short, integer, long Floating point: float, double ...
- Morris.js-利用JavaScript生成时序图
morris.js是一个轻量级的时间序列javascript类库,是网页显示图表的好工具.github项目地址:点击打开,使用起来很简单,但是需要你有一点网页设计的一些基本知识,对一个网页内容的结构要 ...
- Android Asynchronous Http Client
Features Make asynchronous HTTP requests, handle responses in anonymous callbacks HTTP requests happ ...
- Life here can be a dream come true!
Life here can be a dream come true!美梦迟早会成真的!