题意

$n \times n$的网格,有$m$个苹果树,选择一个点出发,每次增加一个偏移量$(dx, dy)$,最大化经过的苹果树的数量

Sol

上面那个互素一开始没看见,然后就GG了

很显然,若$n$和$dx$互素的话,每个$x$都能到达

我们预处理出在每个点$x = 0$时的$y$,取一下最大值即可

求解需要用到扩展欧几里得

  1. /*
  2.  
  3. */
  4. #include<iostream>
  5. #include<cstdio>
  6. #include<cstring>
  7. #include<algorithm>
  8. #include<map>
  9. #include<vector>
  10. #include<set>
  11. #include<queue>
  12. #include<cmath>
  13. //#include<ext/pb_ds/assoc_container.hpp>
  14. //#include<ext/pb_ds/hash_policy.hpp>
  15. #define Pair pair<int, int>
  16. #define MP(x, y) make_pair(x, y)
  17. #define fi first
  18. #define se second
  19. #define int long long
  20. #define LL long long
  21. #define ull unsigned long long
  22. #define rg register
  23. #define pt(x) printf("%d ", x);
  24. //#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? EOF : *p1++)
  25. //char buf[(1 << 22)], *p1 = buf, *p2 = buf;
  26. //char obuf[1<<24], *O = obuf;
  27. //void print(int x) {if(x > 9) print(x / 10); *O++ = x % 10 + '0';}
  28. //#define OS *O++ = ' ';
  29. using namespace std;
  30. //using namespace __gnu_pbds;
  31. const int MAXN = 1e6 + , INF = 1e9 + , mod = 1e9 + ;
  32. const double eps = 1e-;
  33. inline int read() {
  34. char c = getchar(); int x = , f = ;
  35. while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
  36. while(c >= '' && c <= '') x = x * + c - '', c = getchar();
  37. return x * f;
  38. }
  39.  
  40. int N, M, dx, dy, a, b;
  41. int num[MAXN];
  42. int exgcd(int a, int b, int &x, int &y) {
  43. if(!b) {x = ; y = ; return a;}
  44. int r = exgcd(b, a % b, x, y);
  45. int tmp = x; x = y; y = tmp - a / b * y;
  46. return r;
  47. }
  48. main() {
  49. N = read(); M = read(); dx = read(); dy = read();
  50. int ans = ;
  51. int x, y;
  52. for(int i = ; i <= M; i++) {
  53. x = read(), y = read();
  54. if(x == ) {num[y]++; if(num[y] > num[ans]) ans = y;continue;}
  55. int r = exgcd(dx, N, a, b);
  56. a = (a + N) % mod;
  57. a = (a * x) % N;
  58. // a =
  59. y = (y - a * dy % N + N) % N;
  60. num[y]++;
  61. if(num[y] > num[ans]) ans = y;
  62. }
  63. printf("%d %d", , ans);
  64.  
  65. return ;
  66. }
  67. /*
  68.  
  69. */

cf492E. Vanya and Field(扩展欧几里得)的更多相关文章

  1. Root(hdu5777+扩展欧几里得+原根)

    Root                                                                          Time Limit: 30000/1500 ...

  2. 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个点,求射 ...

  3. 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- ...

  4. UVA 10090 Marbles 扩展欧几里得

    来源:http://www.cnblogs.com/zxhl/p/5106678.html 大致题意:给你n个球,给你两种盒子.第一种盒子每个盒子c1美元,可以恰好装n1个球:第二种盒子每个盒子c2元 ...

  5. POJ 1061 青蛙的约会 扩展欧几里得

    扩展欧几里得模板套一下就A了,不过要注意刚好整除的时候,代码中有注释 #include <iostream> #include <cstdio> #include <cs ...

  6. 【64测试20161112】【Catalan数】【数论】【扩展欧几里得】【逆】

    Problem: n个人(偶数)排队,排两行,每一行的身高依次递增,且第二行的人的身高大于对应的第一行的人,问有多少种方案.mod 1e9+9 Solution: 这道题由1,2,5,14 应该想到C ...

  7. poj 2891 扩展欧几里得迭代解同余方程组

    Reference: http://www.cnblogs.com/ka200812/archive/2011/09/02/2164404.html 之前说过中国剩余定理传统解法的条件是m[i]两两互 ...

  8. poj 2142 扩展欧几里得解ax+by=c

    原题实际上就是求方程a*x+b*y=d的一个特解,要求这个特解满足|x|+|y|最小 套模式+一点YY就行了 总结一下这类问题的解法: 对于方程ax+by=c 设tm=gcd(a,b) 先用扩展欧几里 ...

  9. poj 1061 扩展欧几里得解同余方程(求最小非负整数解)

    题目可以转化成求关于t的同余方程的最小非负数解: x+m*t≡y+n*t (mod L) 该方程又可以转化成: k*L+(n-m)*t=x-y 利用扩展欧几里得可以解决这个问题: eg:对于方程ax+ ...

随机推荐

  1. 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$ $ ...

  2. 牛客网训练赛26D(xor)

    题目链接:https://www.nowcoder.com/acm/contest/180/D 线性基的学习:https://www.cnblogs.com/vb4896/p/6149022.html ...

  3. Soup协议-即普通post请求,内容域xml

    1.基础问题 1.1 soup-Simple Object Access Protocal简单对象访问协议 a).承载在http协议之上,http支持传输img/html/文件等,soup请求和响应域 ...

  4. SpringBoot | 第二十四章:日志管理之AOP统一日志

    前言 上一章节,介绍了目前开发中常见的log4j2及logback日志框架的整合知识.在很多时候,我们在开发一个系统时,不管出于何种考虑,比如是审计要求,或者防抵赖,还是保留操作痕迹的角度,一般都会有 ...

  5. SpringBoot | 番外:使用小技巧合集

    前言 最近工作比较忙,事情也比较多.加班回到家都十点多了,洗个澡就想睡觉了.所以为了不断更太多天,偷懒写个小技巧合集吧.之后有时间都会进行文章更新的.原创不易,码字不易,还希望大家多多支持!话不多说, ...

  6. rabbit的简单搭建,java使用rabbitmq queue的简单例子和一些坑

    一 整合 由于本人的码云太多太乱了,于是决定一个一个的整合到一个springboot项目里面. 附上自己的项目地址https://github.com/247292980/spring-boot 以整 ...

  7. Elasticsearch支持的字段类型

    es支持下列简单的字段类型: String: string Whole number: byte, short, integer, long Floating point: float, double ...

  8. Morris.js-利用JavaScript生成时序图

    morris.js是一个轻量级的时间序列javascript类库,是网页显示图表的好工具.github项目地址:点击打开,使用起来很简单,但是需要你有一点网页设计的一些基本知识,对一个网页内容的结构要 ...

  9. Android Asynchronous Http Client

    Features Make asynchronous HTTP requests, handle responses in anonymous callbacks HTTP requests happ ...

  10. Life here can be a dream come true!

    Life here can be a dream come true!美梦迟早会成真的!