题目链接:codeforces 492e vanya and field

留个扩展gcd求逆元的板子。

设i,j为每颗苹果树的位置,因为gcd(n,dx) = 1,gcd(n,dy) = 1,所以当走了n步后,x从0~n-1,y从0~n-1都访问过,但x,y不相同.

所以,x肯定要经过0点,所以我只需要求y点就可以了。

i,j为每颗苹果树的位置,设在经过了a步后,i到达了0,j到达了M.

则有

1----------------------(i + b * dx) % n = 0

2-----------------------(j + b * dy) % n = M % n

则2 * dx - 1 * dy消掉未知数 b.

得方程。          

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const ll mod = 1e9+;
const int maxn = 1e6+;
int vis[maxn];
ll n,m,dx,dy;
ll ex_gcd(ll a,ll b, ll &x, ll &y)
{
if (b == )
{
x = ;
y = ;
return a;
}
else
{
ll gcd = ex_gcd(b, a % b, x, y);
ll t = x;
x = y;
y = t - (a / b) * y;
return gcd;
}
}
int main()
{
cin>>n>>m>>dx>>dy;
ll x,y;
ex_gcd(dx,n,x,y);
while(x<) x+=n;
ll inv = x;
ll ans = ;
ll i,j;
ll mm;
for(int k=;k<m;k++)
{
scanf("%I64d %I64d",&i,&j);
mm = ((dx*j-dy*i)%n+n)%n*inv%n;
vis[mm]++;
}
ll mmm = ;
for(int k=;k<n;k++)
{
if(vis[k]>ans)
{
ans = vis[k];
mmm = k;
}
}
printf("0 %I64d\n",mmm);
return ;
}

codeforces 492E. Vanya and Field(exgcd求逆元)的更多相关文章

  1. CodeForces 492E Vanya and Field (思维题)

    E. Vanya and Field time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. Codeforces 492E Vanya and Field

    E. Vanya and Field time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. 【BZOJ-4522】密钥破解 数论 + 模拟 ( Pollard_Rho分解 + Exgcd求逆元 + 快速幂 + 快速乘)

    4522: [Cqoi2016]密钥破解 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 290  Solved: 148[Submit][Status ...

  4. #6392. 「THUPC2018」密码学第三次小作业 / Rsa (exgcd求逆元+快速幂+快速乘)

    题目链接:https://loj.ac/problem/6392 题目大意:给定五个正整数c1,c2,e1,e2,N,其中e1与e2互质,且满足 c1 = m^e1 mod N c2 = m^e2 m ...

  5. 51nod1256【exgcd求逆元】

    思路: 把k*M%N=1可以写成一个不定方程,(k*M)%N=(N*x+1)%N,那么就是求k*M-N*x=1,k最小,不定方程我们可以直接利用exgcd,中间还搞错了: //小小地讲一下exgcd球 ...

  6. Codeforces Round #280 (Div. 2) E. Vanya and Field 数学

    E. Vanya and Field Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/492/pr ...

  7. 【板子】gcd、exgcd、乘法逆元、快速幂、快速乘、筛素数、快速求逆元、组合数

    1.gcd int gcd(int a,int b){ return b?gcd(b,a%b):a; } 2.扩展gcd )extend great common divisor ll exgcd(l ...

  8. Codeforces Round #280 (Div. 2)E Vanya and Field(简单题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 本场题目都比较简单,故只写了E题. E. Vanya and Field Vany ...

  9. Codeforces Round #280 (Div. 2) E. Vanya and Field 思维题

    E. Vanya and Field time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. 第13章 Swing程序设计----JDialog窗体

    JDialog窗体是Swing组件中的对话框 JDialog窗体的功能是从一个窗体中弹出另一个窗体,就像是在使用IE浏览器时弹出的确定对话框一样. 在应用程序中创建JDialog窗体需要实例化JDia ...

  2. NSBundle的用法

    bundle是一个目录,其中包含了程序会使用到的资源. 这些资源包含了如图像,声音,编译好的代码,nib文件(用户也会把bundle称为plug-in). 对应bundle,cocoa提供了类NSBu ...

  3. s7-300 第9讲 定时器

  4. CentOS6.6 搭建Zabbix_3.0

    公司有下发内网监控服务器的需求 使用zabbix监控 所以这篇文章是讲述的zabbix的搭建 其实网上很多地方都有 可以参考 环境安装 系统环境: # cat /etc/redhat-release ...

  5. Django 用户认证及OneToOneField

    Django 用户认证如果自己不想写 就可以用django自带的认证 首选导入模块 models.py #!/usr/bin/env python #_*_ coding:utf8 _*_ from ...

  6. Python 线程,进程

    Threading用于提供线程相关的操作,线程是应用程序中工作的最小单元 线程不能实现多并发 只能实现伪并发 每次工作 只能是一个线程完成 由于python解释器 原生是c  原生线程 底层都会有一把 ...

  7. SQL语句创建access表

    CREATE TABLE Persons(ID AutoIncrement primary key,Id_P int NOT NULL,LastName varchar(255) NOT NULL,s ...

  8. Mysql转化blob为可读

    -- info 为列名 SELECT convert(info using latin1) FROM drupal755.system; SELECT convert(info using utf8) ...

  9. 【bfs】 poj 3984 maze 队列存储

    #include <iostream> #include <stdio.h> #include <cstring> #define Max 0x7f7f7f7f u ...

  10. Haproxy的安装和配置示例

    1.ha proxy简介ha proxy是一个开源的,高性能的,基于tcp第四层和http第七层应用的负载均衡软件优点:可靠性和稳定性非常好          最高可以同时维护40000-50000个 ...