10673
终于把这道扩展欧几里德的给做出来了。
扩展欧几里德是解决ax+by=c的方程的任意一组解时用的。因为有公理:ax+by=gcd(a,b);
故上述问题中求的x,y分别应该是x*(c/gcd(a,b)),y*(c/gcd(a,b));这就是一般通式的解;
因此解存在的条件是c%gcd(a,b) == 0;
//============================================================================
// Name : 10673.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int N;
int x, y, n, k, t1, t2,t;
int exGcd(int a, int b)
{
if(b == 0)
{
x = 1;
y = 0;
return a;
}
int r = exGcd(b, a%b);
int t = x;
x = y;
y = t - a/b*y;
return r;
}
int main() {
freopen("a.txt", "r", stdin);
scanf("%d", &N);
while(N--){
scanf("%d%d", &n, &k);
t1 = floor(n*1.0/k);
t2 = ceil(n*1.0/k);
t = exGcd(t1, t2);
printf("%d %d\n", x*(n/t), y*(n/t));
}
return 0;
}

浙公网安备 33010602011771号