题目
题解思路
先将同余方程转移
ax mod c=b ⟺ ax=cy+b
再直接根据拓展欧几里得算法,我们可以得到一组特解,再通过这组特解来推出所有解。
下图 通解处 a b 互换 。
这里d为1 a 肯定大于 1 ,这样特解就是 附近的正数就是最大的。
详解
AC代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
using namespace std;
const int INF = 0x3f3f3f3f;
int exgcd( int a ,int b , int & x , int & y )
{
if ( b == 0 )
{
x = 1 ;
y = 0 ;
return a ;
}
int d = exgcd( b , a % b , y , x ) ;
y = y - a/b*x ;
return d ;
}
int main ()
{
ios::sync_with_stdio(false);
int a , b , x , y ;
cin >> a >> b ;
exgcd(a,b,x,y);
cout << (x + b ) % b << "\n";
return 0 ;
}