放假后回来第2天在网吧AC的第一道题。这是一道变形的扩展欧几里得算法题。由题知:n = A%9973 。设9973*y = n;设A/B = x,则A = B*x; 则题目可以转换为: B*x-9973*y = n;对于扩展欧几里得算法的使用还不熟练,要加强联系啦!~
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int n, b;
int ex_gcd(int a, int b, int &x, int &y)
{
if(!b)
{
x = 1; y = 0; return a;
}
else
{
int d = ex_gcd(b, a%b, x, y);
int t = x;
x = y;
y = t-(a/b)*y;
return d;
}
} //ex_gcd
int main()
{
int T;
int a, b;
int x, y, k;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &b);
int d = ex_gcd(b, 9973, x, y);
x *= n/d;
k = 9973/d;
x = (x%k+k)%k; //满足条件的最小值
printf("%d\n", x%9973); //B*x = A, B*x/B = x;
}
return 0;
}
#include <stdlib.h>
#include <string.h>
using namespace std;
int n, b;
int ex_gcd(int a, int b, int &x, int &y)
{
if(!b)
{
x = 1; y = 0; return a;
}
else
{
int d = ex_gcd(b, a%b, x, y);
int t = x;
x = y;
y = t-(a/b)*y;
return d;
}
} //ex_gcd
int main()
{
int T;
int a, b;
int x, y, k;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &b);
int d = ex_gcd(b, 9973, x, y);
x *= n/d;
k = 9973/d;
x = (x%k+k)%k; //满足条件的最小值
printf("%d\n", x%9973); //B*x = A, B*x/B = x;
}
return 0;
}