hdu 1576 A/B



http://acm.hdu.edu.cn/showproblem.php?pid=1576

此题是裸扩展gcd

设A/B=x;

A%9973=n即 A = 9973*k + n 即B*x-9973*k=n 一个基本的同余方程,利用扩展gcd求解x即可。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>

using namespace std;

long long exGcd(long long a, long long b, long long &x, long long &y){
    if(b == 0){
        x = 1;
        y = 0;
        return a;
    }
    long long gcd = exGcd(b, a%b, y, x);
    y -= (a/b) * x;
    return gcd;
}

int main(){
    int cas;
    scanf("%d",&cas);
    while(cas--){
        int n,B;
        scanf("%d%d",&n,&B);
        long long a = B, b = -9973, c = n,x,y;
        long long gcd = exGcd(a,b,x,y);
        if(c % gcd) continue;///证明没有解
        x *= c / gcd;///求出一个特解
        if(b / gcd < 0) b = -b;
        x %= b / gcd;///利用特解求出通解x + t*(b/gcd)
        if(x < 0)   x += b/gcd;///保证解是整数
        printf("%d\n",x % 9973);
    }
    //system("pause");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值