前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >cf492E. Vanya and Field(扩展欧几里得)

cf492E. Vanya and Field(扩展欧几里得)

作者头像
attack
发布2018-09-17 15:37:28
2860
发布2018-09-17 15:37:28
举报

题意

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

Sol

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

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

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

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

代码语言:javascript
复制
/*

*/
#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 + 10, INF = 1e9 + 10, mod = 1e9 + 7;
const double eps = 1e-9;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', 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 = 1; y = 0; 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 = 0;
    int x, y;
    for(int i = 1; i <= M; i++) {
        x = read(), y = read();
        if(x == 0) {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", 0, ans);
    
    return 0;
}
/*

*/
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-09-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题意
  • Sol
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档