当前位置:网站首页>求解同余方程 数论 扩展欧几里得
求解同余方程 数论 扩展欧几里得
2022-08-03 23:58:00 【Rachel caramel】
题面:

思路:
a ⋅ x = b ⋅ y + 1 a ⋅ x − b ⋅ y = 1 因为y的取值是任意的,因此可以将这个式子看成: a ⋅ x + b ⋅ y = 1 因为题目保证有解,所以a和b是互质的,即 gcd ( a , b ) = 1 而扩展欧几里得就是用来求形如: a ⋅ x + b ⋅ = c 的不定方程的整数解的 这里需要再补充一个裴蜀定理: 若 a , b 为整数,那么一定存在 a ⋅ x + b ⋅ y = gcd ( a , b ) 即 a ⋅ x + b ⋅ y 的值一定是 gcd ( a , b ) 的倍数 先考虑边界情况 a ⋅ 1 + b ⋅ 0 = gcd ( a , b ) , 此时, x = 1 , y = 0 然后考虑一般情况,假设某一次得到的解是 x 0 、 y 0 b ⋅ x 0 + ( a m o d b ) ⋅ y 0 = gcd ( a , b ) b ⋅ x 0 + ( a − ⌊ a b ⌋ ⋅ b ) ⋅ y 0 = gcd ( a , b ) a ⋅ y 0 + b ⋅ ( x 0 − ⌊ a b ⌋ ⋅ y 0 ) = gcd ( a , b ) 由此可得: x = y 0 , y = x 0 − ⌊ a b ⌋ ⋅ y 0 已知任意一组解 x 0 , y 0 通解为: x = x 0 + b gcd ( a , b ) ⋅ n x = x 0 + b gcd ( a , b ) ⋅ n a\cdot x=b\cdot y +1\\ a\cdot x-b\cdot y =1\\ \text{因为y的取值是任意的,因此可以将这个式子看成:}\\ a\cdot x+b\cdot y =1\\ \text{因为题目保证有解,所以a和b是互质的,即}\gcd(a,b)=1\\ \text{而扩展欧几里得就是用来求形如:} a\cdot x+b\cdot=c\text {的不定方程的整数解的}\\ \text{这里需要再补充一个裴蜀定理:}\\ \text{若}a,b\text{为整数,那么一定存在}a\cdot x+b\cdot y=\gcd(a,b)\\ \text{即}a\cdot x+b\cdot y \text{的值一定是}\gcd(a,b)\text{的倍数}\\ \text{先考虑边界情况}a\cdot 1+b\cdot 0=\gcd(a,b),\text{此时,}x=1,y=0\\ \text{然后考虑一般情况,假设某一次得到的解是}x_0、y_0\\ b\cdot x_0+(a\bmod b)\cdot y_0=\gcd(a,b)\\ b\cdot x_0+(a-\lfloor \frac a b \rfloor\cdot b)\cdot y_0=\gcd(a,b)\\ a\cdot y_0+b\cdot (x_0-\lfloor \frac a b \rfloor\cdot y_0)=\gcd(a,b)\\ \text{由此可得:} x=y_0 ,y=x_0-\lfloor \frac a b \rfloor\cdot y_0\\ \text{已知任意一组解}x_0,y_0\text{通解为:} \\ x=x0+ \frac{b} {\gcd (a,b)}\cdot n\\ x=x0+ \frac{b} {\gcd (a,b)}\cdot n\\ a⋅x=b⋅y+1a⋅x−b⋅y=1因为y的取值是任意的,因此可以将这个式子看成:a⋅x+b⋅y=1因为题目保证有解,所以a和b是互质的,即gcd(a,b)=1而扩展欧几里得就是用来求形如:a⋅x+b⋅=c的不定方程的整数解的这里需要再补充一个裴蜀定理:若a,b为整数,那么一定存在a⋅x+b⋅y=gcd(a,b)即a⋅x+b⋅y的值一定是gcd(a,b)的倍数先考虑边界情况a⋅1+b⋅0=gcd(a,b),此时,x=1,y=0然后考虑一般情况,假设某一次得到的解是x0、y0b⋅x0+(amodb)⋅y0=gcd(a,b)b⋅x0+(a−⌊ba⌋⋅b)⋅y0=gcd(a,b)a⋅y0+b⋅(x0−⌊ba⌋⋅y0)=gcd(a,b)由此可得:x=y0,y=x0−⌊ba⌋⋅y0已知任意一组解x0,y0通解为:x=x0+gcd(a,b)b⋅nx=x0+gcd(a,b)b⋅n
代码:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
const int maxn=111111;
int a,b,x,y;
void exgcd(int a,int b,int *x,int *y)//扩展欧几里得算法
{
//cout<<"a="<<a<<" "<<"b="<<b<<" "<<"x="<<*x<<" "<<"y="<<*y<<endl;
if(b==0)
{
*x=1,*y=0;
return;
}
exgcd(b,a%b,x,y); //r=GCD(a,b)=GCD(b, a%b)
//cout<<"!!!a="<<a<<" "<<"b="<<b<<" "<<"x="<<*x<<" "<<"y="<<*y<<endl;
int t=*x;
*x=*y;
*y=t-a/b*(*y) ;
}
int main()
{
cin>>a>>b;
exgcd(a,b,&x,&y);
//cout<<"x="<<x<<" "<<"b="<<b<<endl;
while(x<0) x+=b;
cout<<x<<endl;
return 0;
}
边栏推荐
- YOLOv7改进之二十二:涨点神器——引入递归门控卷积(gnConv)
- BPF 可移植性和 CO-RE(一次编译,到处运行)
- Fluorescein-PEG-CLS, cholesterol-polyethylene glycol-fluorescein scientific research reagent
- Justin Sun was invited to attend the 36氪 Yuan Universe Summit and delivered a keynote speech
- 禾匠编译错误记录
- leetcode/子串中不能有重复字符的最长子串
- sqlnet.ora文件与连接认证方式的小测试
- After building the pytorch environment, the pip and conda commands cannot be used
- JVM垃圾回收总结(未完待续)
- Apple told Qualcomm: I bought a new campus for $445 million and may plan to speed up self-development of baseband chips
猜你喜欢

关于mnn模型输出的数据杂乱无章问题

The Beijing E-sports Metaverse Forum was successfully held

After building the pytorch environment, the pip and conda commands cannot be used

RSS feeds WeChat public - feed43 asain

It will invest about 200 billion US dollars in the United States in 20 years, and Samsung Electronics looks so handsome

Creo 9.0在草图环境中创建坐标系

数据分析知识点搜集(纯粹的搜集)

超级完美版布局有快捷键,有背景置换

用队列模拟实现栈

智能座舱的「交互设计」大战
随机推荐
(PC+WAP)织梦模板不锈钢类网站
超级完美版布局有快捷键,有背景置换(解决opencv 中文路径问题)
查看CUDA、pytorch等的版本号
Salesforce's China business may see new changes, rumors may be closing
免费的公共WiFi不要乱连,遭中间人攻击了吧?
Apple told Qualcomm: I bought a new campus for $445 million and may plan to speed up self-development of baseband chips
搭建好pytorch环境后,pip和conda指令不能用
Justin Sun: Web3.0 and the Metaverse will assist mankind to enter the online world more comprehensively
Using matlab to solve the linear optimization problem based on matlab dynamic model of learning notes _11 】 【
Creo 9.0二维草图的诊断:着色封闭环
Sqlnet. Ora file with the connection of authentication test
C语言实验十四 结构体
Unity2021 releases WebGL fog effect disappearing problem
V8中的快慢数组(附源码、图文更易理解)
汉字风格迁移---结合本地和全局特征学习的中文字体迁移
BioVendor人Clara细胞蛋白(CC16)Elisa试剂盒检测步骤
win10+cuda11.7+pytorch1.12.0 installation
小米--测试开发
单例模式使用饿汉式和懒汉式创建一定安全?很多人不知
七夕活动浪漫上线,别让网络拖慢和小姐姐的开黑时间