UVaOJ10673 - Play with Floor and Ceil
2013-07-07 15:41
253 查看
10673 - Play with Floor and Ceil
Time limit: 3.000 secondsTheorem
For any two integers x and k there exists two more integers p and q such that:It’s a fairly easy task to prove this theorem, so we’d not ask you to do that. We’d ask for something even easier! Given the values of x and k, you’d only need to find integers p and q that
satisfies the given equation.
Input
The first line of the input contains an integer, T (1≤T≤1000) that gives you the number of test cases. In each of the following T lines you’d be given two positive integers x and k. You
can safely assume that x and k will always be less than 108.
Output
For each of the test cases print two integers: p and q in one line. These two integers are to be separated by a single space. If there are multiple pairs of p and q that satisfy the equation,any one would do. But to help us keep our task simple, please make sure that the values,
and
fit
in a 64 bit signed integer.
Sample Input Output for Sample Input
3 5 2 40 2 24444 6 | 1 1 1 1 0 6 |
Special Thanks: Shahriar Manzoor, Member of Elite Problemsetters' Panel
题目大意:
给出x和k,求p*floor(x/k)+q*ceil(x/k)==x的一组解(p,q)解题方法:
我们知道向上取整和向下取整之差只能为0或1,如果为1,我们显然可以找到p=x,q=-x为符合要求的一组解,如果为0,那么只要(p+q)[x/k]=x,同时注意到在这种情况下x%k==0,所以令p+q=k即可,如果想让p、q有整数解,最简单的办法就是p=0,q=k。源代码:
#include <iostream> #include <cstdio> using namespace std; int main() { int T, a, b; long x, k, p, q; scanf("%d", &T); while (T --) { scanf("%ld%ld", &x, &k); a = x / k; b = x / k + (x % k ? 1 : 0); if (a != b) { p = -x; q = x; } else { p = 0; q = k; } printf("%ld %ld\n", p, q); } return 0; }
相关文章推荐
- Play with Floor and Ceil
- 描述算法10673 - Play with Floor and Ceil
- UVa - 10673 - Play with Floor and Ceil
- UVA10673 - Play with Floor and Ceil(数论)
- UVA 10673 Play with Floor and Ceil (扩展欧几里德)
- UVA 10673 Play with Floor and Ceil
- UVa 10673 - Play with Floor and Ceil
- UVA - 10673 Play with Floor and Ceil
- uva10673 - Play with Floor and Ceil(floor和ceil)
- 10673 Play with Floor and Ceil 简单数学
- UVa 10673 - Play with Floor and Ceil (扩展欧几里得)
- UVa 10673 Play with Floor and Ceil (数论)
- uva 10673 Play with Floor and Ceil
- uva 10673 Play with Floor and Ceil(扩展gcd)
- uva 10673 - Play with Floor and Ceil(欧几里得算法)
- Play with Floor and Ceil - UVa 10673
- UVA10673 - Play with Floor and Ceil
- uva10673 - Play with Floor and Ceil 扩展欧几里德算法
- [UVa 10673]Play with Floor and Ceil
- uva 10673 ——Play with Floor and Ceil