codeforces 492E. Vanya and Field(exgcd求逆元)
题目链接: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求逆元)的更多相关文章
- CodeForces 492E Vanya and Field (思维题)
E. Vanya and Field time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 492E Vanya and Field
E. Vanya and Field time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 【BZOJ-4522】密钥破解 数论 + 模拟 ( Pollard_Rho分解 + Exgcd求逆元 + 快速幂 + 快速乘)
4522: [Cqoi2016]密钥破解 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 290 Solved: 148[Submit][Status ...
- #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 ...
- 51nod1256【exgcd求逆元】
思路: 把k*M%N=1可以写成一个不定方程,(k*M)%N=(N*x+1)%N,那么就是求k*M-N*x=1,k最小,不定方程我们可以直接利用exgcd,中间还搞错了: //小小地讲一下exgcd球 ...
- 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 ...
- 【板子】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 ...
- Codeforces Round #280 (Div. 2)E Vanya and Field(简单题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 本场题目都比较简单,故只写了E题. E. Vanya and Field Vany ...
- 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 ...
随机推荐
- csuoj1009
AC代码: #include <iostream>#include <iomanip>using namespace std;//计算数学期望值,可以自己直接通过数组的方式来实 ...
- Angularjs中的ng-class
在angular中为我们提供了3种方案处理class:1:scope变量绑定.(不推荐使用)2:字符串数组形式.3:对象key/value处理. 我们继续其他两种解决方案:1字符串数组形式是针对cla ...
- laravel使用多个数据库连接
1.配置.env文件 DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=database_name DB_USERNAME= ...
- 微信支付WxpayAPI_php_v3(二)支付功能开发
这里我没有开发openid和acessToken的获取,需要的请参考文档获取. 在阅读本教程之前请熟读微信支付的开发者文档. 直接开始[统一下单],在实际开发的项目中一般都有mvc分层的开发思想. 根 ...
- Windows下动态库的编译以及调用
1.MFC下生成动态库 1>显式调用 在.cpp文件里添加接口函数 int sum(int a,int b) { return a + b; } int sub(int a,int b) { r ...
- Android OpenGL ES(二)OpenGL ES管道(Pipeline) .
大部分图形系统都可以比作工厂中的装配线(Assemble line)或者称为管道(Pipeline).前一道的输出作为下道工序的输入.主CPU发出一个绘图指令,然后可能由硬件部件完成坐标变换,裁剪,添 ...
- 第一个前台页面----xflow的页面
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><%@ tagl ...
- NSObject Class 浅析
Objective-C中有两个NSObject,一个是NSObject类,另一个是NSObject协议.而其中NSObject类采用了NSObject协议.在本文中,我们主要整理一下NSObject类 ...
- 添加一个Application Framework Service
如何添加一个Application Framework Service(without native code)? 1.本文参照AlarmManagerService实现一个简单的Applicatio ...
- linux内核移植到S5pv210
make s5pv210_defconfig 1.System Type ---> (0) S3C UART to use for low-level messages 2.Kernel ha ...