LuoguP1516 青蛙的约会 (Exgcd)

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); (a) <= (c); ++(a))
#define nR(a,b,c) for(register int a = (b); (a) >= (c); --(a))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))

#define ON_DEBUGG

#ifdef ON_DEBUGG

#define D_e_Line printf("\n----------\n") 
#define D_e(x) cout << (#x) << " : " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt", "r", stdin)

#else

#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;

#endif
using namespace std;
struct ios{
    template<typename ATP>inline ios& operator >> (ATP &x){
        x = 0; int f = 1; char ch;
        for(ch = getchar(); ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1;
        while(ch >= '0' && ch <= '9') x = x * 10 + (ch ^ '0'), ch = getchar();
        x *= f;
        return *this;
    }
}io;

#define int long long
inline int Gcd(int a, int b){
    while(b ^= a ^= b ^= a %= b);
    return a;
//  if(!b) return a;
//  return Gcd(b, a % b);
}
inline void Exgcd(int a, int b, int &x, int &y){
    if(!b)
        x = 1, y = 0;
    else
        Exgcd(b, a % b, y, x), y -= x * (a / b);
}
#undef int 
int main(){
#define int long long
    int a, b, m, n, L;
    io >> a >> b >> m >> n >> L;
    int A = n - m, B = L, C = a - b; 
    if(A < 0){
        A = -A, C = -C;
    }
    int r = Gcd(A, B);
    if(C % r){
        printf("Impossible");
        return 0;
    }
    A /= r, B /= r, C /= r;
    //D_e(B);
    int x, y;
    Exgcd(A, B, x, y);
    
//  cout << x * C << endl;
//  cout << x * C % B << endl;
//  cout << x * C % B + B << endl;
    printf("%lld", (x * C  % B + B) % B);
    
    return 0;
}
/*
(n - m) * x + L * y = a - b

A = n - m
B = L
C = A - B
*/

猜你喜欢

转载自www.cnblogs.com/bingoyes/p/11368709.html