hdu 1576(A/B)

题目链接:点击打开链接

题目是汉语的,我就不写题目大意了;


题目分析:首先,这道题涉及扩展欧几里得算法; 求(A/B)%9973 , 设 (A/B)%9973 = k    ==>     (A/B) = k+9973a;    ==>     (1) A = (k+9973t)*B;     因为 A%9973 == n  所以 

(2) n + 9973b = A ;   联立(1) (2)可得  n + 9973b = (k+9973t)*B   ==>   n = Bk + 9973(tB-b);   现在变为了扩展欧几里得算法的标准用法;


#include 
#include 
#include 
#include 

using namespace std;

#define LL long long
LL n, b;

void gcd(LL a, LL b, LL &d, LL &x, LL &y){
    if(!b) { d = a; x = 1; y = 0; }
    else { gcd(b, a%b, d, y, x); y -= x*(a/b); }
}

int main()
{
    int T;
    scanf("%d", &T);
    while(T--){
        scanf("%I64d%I64d", &n, &b);
        LL d, x, y;
        gcd(b, 9973, d, x, y);
        x *= n/d;       //x是可能的一个答案, d为1;
        // 答案范围为  x + k*b';  b' == 9973/d;
        x %= 9973;
        if(x < 0) x += 9973;
        printf("%I64d\n", x);
    }
    return 0;
}


你可能感兴趣的:(hdu 1576(A/B))