关于Exgcd
%%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;
}
#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的更多相关文章
- 扩展欧几里得 exGCD
Elementary Number Theory - Extended Euclid Algorithm Time Limit : 1 sec, Memory Limit : 65536 KB Jap ...
- NOIP2012同余方程[exgcd]
题目描述 求关于 x 的同余方程 ax ≡ 1 (mod b)的最小正整数解. 输入输出格式 输入格式: 输入只有一行,包含两个正整数 a, b,用一个空格隔开 输出格式: 输出只有一行,包含一个正整 ...
- exgcd,求乘法逆元
procedure exgcd(a,b:int64); var t:longint; begin then begin x:=;y:=; exit; end else exgcd(b,a mod b) ...
- 【板子】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 ...
- 【BZOJ-4522】密钥破解 数论 + 模拟 ( Pollard_Rho分解 + Exgcd求逆元 + 快速幂 + 快速乘)
4522: [Cqoi2016]密钥破解 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 290 Solved: 148[Submit][Status ...
- poj1061 Exgcd
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> usin ...
- 51Nod 1256 乘法逆元 Label:exgcd
1256 乘法逆元 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 给出2个数M和N(M < N),且M与N互质,找出一个数K满足0 < K < N且K ...
- 【BZOJ2242】【SDoi2011】计算器 快速幂+EXGCD+BSGS
Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数: 3.给 ...
- Poj 2115 C Looooops(exgcd变式)
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22704 Accepted: 6251 Descripti ...
- 【BZOJ 1319】 Sgu261Discrete Rootsv (原根+BSGS+EXGCD)
1319: Sgu261Discrete Roots Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 389 Solved: 172 Descriptio ...
随机推荐
- 企业微信同步LDAP
1.需求 定期同步企业微信的用户信息到 LDAP 中,当有新用户时,会自动发送LDAP的账号密码给该用户邮箱. 2.环境 python 3.x 需要安装两个模块 pip install ldap3 r ...
- 中文情感分析——snownlp类库 源码注释及使用
最近发现了snownlp这个库,这个类库是专门针对中文文本进行文本挖掘的. 主要功能: 中文分词(Character-Based Generative Model) 词性标注(TnT 3-gram 隐 ...
- vue router 常用操作
1. 普通路由 const routes = [ { path: '/index', component: index } ] 2. 重定向 redirect const routes = [ { ...
- 理解了quote和 symbol-list的 关系
'x === (quote x) '(x) === (list 'x)
- 高德地图的JSAPI学习笔记【一】
高德地图的项目要做 学习笔记记录下来 一.注册账号并申请Key 二.准备页面写好 1.在页面添加 JS API 的入口脚本标签,并将其中「您申请的key值」替换为您刚刚申请的 key: <sc ...
- Composer安装laravel框架
一.打开CMD,进入想安装的目录,输入如下图所示,安装一个blog的项目: 二.进入指定目录即可看到生成的blog项目,如下图:
- SQL报错注入
0x00:前言 sqli-libs第11关的报错注入,之前没有具体学习了解过,所以单独学习一下. 0x01:例子 uname=1&passwd=1' union select count(*) ...
- 中文版Postman测试需要登陆才能访问的接口(基于Cookie)
ApiPost堪称增强版的中文postman,是一个支持团队协作,并可直接生成文档的API调试.管理工具.它支持模拟POST.GET.PUT等常见请求,是后台接口开发者或前端.接口测试人员不可多得的工 ...
- wait waitpid
定义 pid_t wait(int *status); pid_t waitpid(pid_t pid, int *status, int options); 暂时停止进程的执行,直到有信号来到或子进 ...
- webview学习
Android中WebView使用6,js调java实现播放视频 https://blog.csdn.net/zhaihaohao1/article/details/77993890 android ...