%%lkx

学习博客

exgcd(扩展欧几里得)

可以用来判断并求解形如\(ax+by=c\)的方程,当且仅当\(gcd(a,b)|c\)时,存在整数解\(x,y\)

也就是说,\(exgcd\)可以用来求解方程\(ax+by=gcd(a,b)\),令\(a=b,b=a\%b\)则有方程\(b*x_1+(a\%b)*y_1=gcd(b,a\% b)\)

又因为\(gcd(a,b)=gcd(b,a\%b)\),且\(a\%b=a-b*\) \(\lfloor {a/b}\rfloor * y_1=gcd(a,b)\)

整理得:\(ay_1+b(x_1-\lfloor {a/b}\rfloor* y_1)=gcd(a,b)\)

所以原方程中: \(ay_1+ b(x_1-\lfloor {a/b}\rfloor *y_1)=gcd(a,b)\)

所以我们只需递归求\(x_1,y_1\),就能求出\(x,y\)

Code:

void exgcd(int a, int b, int &x, int &y) {
if(!b) {x = 1, y = 0;return;}
int d = exgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}

例:洛谷p1082同余方程

#include <cstdio>
#include <iostream>
using namespace std;
int a, b, x, y;
int read() {
int s = 0, w = 1;
char ch = getchar();
while(!isdigit(ch)) {if(ch == '-') w = -1; ch = getchar();}
while(isdigit(ch)) {s = s * 10 + ch - '0'; ch = getchar();}
return s * w;
}
void exgcd(int a, int b, int &x, int &y) {
if(!b) x = 1, y = 0;
else exgcd(b, a % b, y, x), y -= a / b * x;
}
int main() {
a = read(), b = read();
exgcd(a, b, x, y);
cout << (x + b) % b << endl;
return 0;
}

谢谢收看, 祝身体健康!

关于Exgcd的更多相关文章

  1. 扩展欧几里得 exGCD

    Elementary Number Theory - Extended Euclid Algorithm Time Limit : 1 sec, Memory Limit : 65536 KB Jap ...

  2. NOIP2012同余方程[exgcd]

    题目描述 求关于 x 的同余方程 ax ≡ 1 (mod b)的最小正整数解. 输入输出格式 输入格式: 输入只有一行,包含两个正整数 a, b,用一个空格隔开 输出格式: 输出只有一行,包含一个正整 ...

  3. exgcd,求乘法逆元

    procedure exgcd(a,b:int64); var t:longint; begin then begin x:=;y:=; exit; end else exgcd(b,a mod b) ...

  4. 【板子】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 ...

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

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

  6. poj1061 Exgcd

    #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> usin ...

  7. 51Nod 1256 乘法逆元 Label:exgcd

    1256 乘法逆元 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 给出2个数M和N(M < N),且M与N互质,找出一个数K满足0 < K < N且K ...

  8. 【BZOJ2242】【SDoi2011】计算器 快速幂+EXGCD+BSGS

    Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数: 3.给 ...

  9. Poj 2115 C Looooops(exgcd变式)

    C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22704 Accepted: 6251 Descripti ...

  10. 【BZOJ 1319】 Sgu261Discrete Rootsv (原根+BSGS+EXGCD)

    1319: Sgu261Discrete Roots Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 389  Solved: 172 Descriptio ...

随机推荐

  1. IOS手机 html5页面 数字变成蓝色链接的原因

    IOS手机 html5页面 数字变成蓝色链接的原因 这个是ios手机自动识别 写如下代码 即可<pre> <meta name="format-detection" ...

  2. Go - chan 通道

    概述 原来分享的基础语法的时候,还未分享过 chan 通道,这次把它补上. chan 可以理解为队列,遵循先进先出的规则. 在说 chan 之前,咱们先说一下 go 关键字. 在 go 关键字后面加一 ...

  3. 查看索引在哪些ES集群节点上的命令

    用途:迁移数据到其他节点上的时候需要用这个 GET /_cat/shards GET /_cat/shards?h=n

  4. Linq 将两个查询结果合称为一个

    var handsonitems = from a in db.DltQuestionHandson join c in db.DltBdChapter on new { a.ChapterCode ...

  5. wget下载阿里云oss的文件报错403

    问题 在实际工作中,我们为了方便,会将一些脚本储存在云端(阿里云OSS),这样方便我们使用和下载,但是在实际的使用过程中,我们会遇到一些问题. 示例链接:https://djxlsp.oss-cn-s ...

  6. How to call a stored procedure in EF Core 3.0 via FromSqlRaw(转载)

    问: I recently migrated from EF Core 2.2 to EF Core 3.0. Unfortunately, I haven't found a way to call ...

  7. vs 发版时,在发版的文件夹中,找不到应该有的某个文件

    检查:VS中,这个文件右击属性,查看生成操作.如果是“无”,改为“内容”.再重新发布就没问题了. 想看发版出来的内容包括哪些,可以从“发布”--“应用程序文件”查看

  8. Linq与委托

    using System; using System.Linq; using System.Reflection; using Stuglxt_Models; namespace ConsoleApp ...

  9. JAVA中调用外部程序,并等待其退出(涉及Runtime和ProcessBuilder)

    这段时间要写一个java调用外部程序的功能,踩了几个坑,这里分享一下. 首先用的是RunTime,调用代码如下: Process pro = Runtime.getRuntime().exec(&qu ...

  10. Qt keyevent学习笔记

    在按下一个键不放后,会发生: 1.触发keypressevent(),此时isautorepeat()返回false: 2.set isautorepeat(),使其返回值为true; 3.触发key ...