HDU - 1576 A/B (扩展欧几里得应用)

版权声明:希望能在自己成长的道路上帮到更多的人,欢迎各位评论交流 https://blog.csdn.net/yiqzq/article/details/82085646

原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=1576

思路:
(1) 由题可知:读入的是n和B,且gcd(B,9973) = 1 ;
(2)假设(A/B)%9973 = k ; 所以 A/B = k + 9973*x —> A = Bk + Bx*9973 ;
且n = A % 9973 ;替换可得 : n = (Bk +Bx*9973)%9973
(3)化简上式,因为是9973的倍数,所以取余为0 , 得: Bk % 9973 = n ;
(4)同(2)一样, 化简(3)式 , Bk = n + 9973* t ;两边同除n并且移相可得 —> Bk/n-9973*t/n = 1 ;
(5)其符合 (k/n)*B - (t/n)*9973 = gcd(B,9973) = 1 ;
故求解所得 x , y 即为 x = (k/n) , y = -(t/n);

然后最后套用板子算出来的就是x,那么我们答案是k,只需要x*n即可.

最后附上一个推论,若 a x + b y = c 的一组整数解是 ( x 0 , y 0 ) ,那么他的任意整数解是 ( x 0 + k b 1 , y 0 k a 1 )
a 1 = a / g c d ( a , b )
b 1 = b / g c d ( a , b )

#include <bits/stdc++.h>
#define eps 1e-8
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define lson l,mid,rt<<1
#define rson mid+1,r,(rt<<1)+1
#define CLR(x,y) memset((x),y,sizeof(x))
#define fuck(x) cerr << #x << "=" << x << endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int seed = 131;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
ll exgcd(ll a, ll b, ll &x, ll &y) { //求解ax+by=gcd(a,b)
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    ll ans = exgcd(b, a % b, x, y);
    ll temp = x;
    x = y;
    y = temp - (a / b) * y;
    return ans;
}


int main() {
    ll t;
    scanf("%lld", &t);
    while (t--) {
        ll n, b, x, y;
        scanf("%lld%lld", &n, &b);
        exgcd(b, 9973, x, y);
        ll ans = (x * n % 9973 + 9973) % 9973;
        printf("%lld\n", ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yiqzq/article/details/82085646