[HDU] [1576] A/B

Problem Description

Loading...

Problem Analysis

A problem I saw quite a while ago
It involves the Extended Euclidean Algorithm
that is
ax+by=gcd(a,b), solve for x and y

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<stdio.h>
int x,y;
void gcd(int a,int b) {
    int t;
    if (b==0) {
        x=1;
        y=0;
        return;
    } else {
        gcd(b,a%b);
        t=x;
        x=y;
        y=t-(a/b)*y;
    }
}
int main() {
    int T;
    scanf("%d",&T);
    while(T--) {
        int n,m;
        scanf("%d %d",&n,&m);
        gcd(m,9973);
        if (x<0)
            x+=9973;
        x*=n;
        printf("%d\n",x%9973);
    }
    return 0;
}

Problem link: [HDU] [1576] A/B

Loading...