洛谷P1082 同余方程 数论
生活随笔
收集整理的這篇文章主要介紹了
洛谷P1082 同余方程 数论
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
洛谷P1082 同余方程
數論
要求 ax === 1 (mod b) 相當于求 ax + by == 1 的解
并要求 x 為最小的正整數
這樣我們只要 擴展歐幾里德來一發,然后最小正整數 取 mod 就行了
?
但是一般題目里會讓你求一個最小的x,當你用拓歐求出一個解時,一般會讓你去找一個最小解,
我們只需要對這個數取模b就行了(如果求正數,你只需要先加一個b,再取模行了,應該都知道吧)
?
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <algorithm> 6 #include <iomanip> 7 #include <iostream> 8 using namespace std ; 9 10 int n,m,x,y,a,b,t,ans ; 11 12 inline int gcd(int a,int b ) 13 { 14 if(!b) 15 { 16 x = 1,y = 0 ; 17 return a ; 18 } 19 20 int tmp = gcd(b,a%b) ; 21 t = x ; 22 x = y ; 23 y = t - a/b*y ; 24 return tmp ; 25 } 26 27 int main() 28 { 29 scanf("%d%d",&a,&b) ; 30 ans = gcd(a,b) ; 31 if(x<0) x+=b ; 32 printf("%d\n",x) ; 33 34 return 0 ; 35 }?
轉載于:https://www.cnblogs.com/third2333/p/6927070.html
總結
以上是生活随笔為你收集整理的洛谷P1082 同余方程 数论的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C嵌入汇编
- 下一篇: 【小工匠聊Modbus】05-数据类型