uva 10673 - Play with Floor and Ceil(欧几里得算法)

题目链接:uva 10673 - Play with Floor and Ceil


题目大意:给出x 和k,求解p和q使得等式x = p[x / k] + q [ x / k], 两个[x / k]分别为向下取整和向上取整。


解题思路:欧几里得算法求解二元一次方程的解。


#include <stdio.h>
#include <math.h>

void gcd(long long a, long long b, long long& d, long long& x, long long& y) {
	if (!b) { d = a, x = 1, y = 0; }
	else { gcd(b, a % b, d, y, x); y -= x * (a / b); }
}

int main () {
	int cas;
	scanf("%d", &cas);
	while (cas--) {
		long long c, k, a, b, d, x, y;
		scanf("%lld%lld", &c, &k);
		a = floor(1.0 * c / k);
		b = ceil(1.0 * c / k);
		gcd(a, b, d, x, y);
		x *= c / d;
		y *= c / d;
		printf("%lld %lld\n", x, y);

	}
	return 0;
}


你可能感兴趣的:(uva 10673 - Play with Floor and Ceil(欧几里得算法))