1. 程式人生 > >hdu 1576 擴展歐幾裏得

hdu 1576 擴展歐幾裏得

clu blog space color stream 歐幾裏德算法 names 求解 while

(A/B)%9973=K

A/B=K+9973*X

A=BK+9973*X*B

A%9973=n;

BK%9973=n;

BK=n+9973*Y

(K/n)*B+(-Y/n)*9973=GCD(B,9973)=1;

求出k/n,求出k

 1 /*
 2     擴展歐幾裏得
 3     擴展歐幾裏德算法是用來在已知a, b求解一組x,y使得ax+by = Gcd(a, b) =d(解一定存在,根據數論中的相關定理)
 4 */
 5 #include<cstdio>
 6 #include<iostream>
 7 #define ll long long
 8 
 9 using
namespace std; 10 11 ll x,y; 12 void gcd(ll a,ll b) 13 { 14 if(b==0) 15 { 16 x = 1; 17 y = 0; 18 return ; 19 } 20 gcd(b,a%b); 21 ll t = x; 22 x = y; 23 y = t-(a/b)*y; 24 } 25 int main() 26 { 27 int t; 28 int n,b; 29 cin>>t; 30 while
(t--) 31 { 32 cin>>n>>b; 33 gcd(b,9973); 34 //x*=n; 35 x = (x%9973+9973)%9973; //最小正數 36 cout<<(x*n)%9973<<endl; 37 } 38 return 0; 39 }

hdu 1576 擴展歐幾裏得