题目传送门
解题思路:
因为推导过程过于复杂,懒得写,所以题解传送门
对于代码第23行答案处理题解中没有给出推导证明,所以我来给出证明。
设x=kb+q,所以x%b=q,所以x=(q+b)%b=(q%b+b%b)%b=q%b。因为q<b,所以q%b=q,即x=q,所以x=x-kb。
AC代码:
1 #include<iostream> 2 #include<cstdio> 3 4 using namespace std; 5 6 long long a,b,x,y; 7 8 void exgcd(long long p,long long o) { 9 if(o == 0) { 10 x = 1; 11 y = 0; 12 return ; 13 } 14 exgcd(o,p % o); 15 long long tx = x; 16 x = y; 17 y = tx - p / o * y; 18 } 19 20 int main() { 21 cin >> a >> b; 22 exgcd(a,b); 23 x = (x % b + b) % b; 24 printf("%lld",x); 25 return 0; 26 }
//NOIP提高2012 Day2 T1