HDU 1576 A/B

来源:互联网 发布:淘宝如何打新品标 编辑:程序博客网 时间:2024/06/02 06:41

扩展欧几里得,逆推

#include <stdio.h>#include <stdlib.h>#include <string.h>#define Mod 9973using namespace std;int n,b,d;int x,y;void ex_gcd(int a, int b, int &x, int &y){    if(!b)    {        x = 1;        y = 0;        d=a;        return;    }        ex_gcd(b, a%b, x, y);        int t = x;        x = y;        y = t-(a/b)*y;        return;}int main(){    int T;    int a,b,k;    scanf("%d", &T);    while(T--)    {        scanf("%d%d", &n, &b);        ex_gcd(b, Mod, x, y);        x*= n/d;        k = Mod/d;        x = (x%k+k)%k;          printf("%d\n", x%Mod);    }    return 0;}