Noip 2012 同余方程

#include 
#include 
#include 
using namespace std;
const int MAXN = 100010;
int a, b, ans;
int x,y;
int ex_gcd(int a, int b, int &x, int &y) {
    if(b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    int ans = ex_gcd(b,a%b,x ,y);
    int temp = x;
    x = y;
    y = temp - (a/b)*y;
    return ans;
} 
int calc(int a,int m) {
    int gcd = ex_gcd(a,m,x,y);
    return gcd == 1 ? (x+m)%m : -1;
}
int main() {
    cin >> a >> b;
    cout << calc(a,b); 
    return 0;
} 

你可能感兴趣的:(NOIP,数学)