首页 > 代码库 > UVa 10673 - Play with Floor and Ceil

UVa 10673 - Play with Floor and Ceil

题目:方程 x = p * floor(x÷k)+ q * ceil(x÷k)给定x和k,求一组成立的p与q。

分析:数论。设 x = m*k + n(其中,n = x mod k);

           若n = 0:x = m*k,floor(x÷k)= ceil(x÷k)= m,x = p * m + q * m,令p = k,q = 0即可;

           若n ≠ 0:x = m*k + n,ceil(x÷k)= m+1,floor(x÷k) = m,x = (p+q)* m + q,

                          此时,令p+q = k,q = n即可(因为n < k,所以一定成立),得p = k - n即可。

说明:当x整除k时,p和q可以互换。

#include <iostream>
#include <cstdlib>
#include <cstdio>

using namespace std;

int main()
{
	int n,x,k;
	while (~scanf("%d",&n))
	for (int t = 0 ; t < n ; ++ t) {
		scanf("%d%d",&x,&k);
		if (x%k)
			printf("%d %d\n",k-x%k,x%k);
		else printf("0 %d\n",k);
	}
	return 0;
}

UVa 10673 - Play with Floor and Ceil