UVa - 10673 - Play with Floor and Ceil
先上题目
Problem A
Play with Floor and Ceil
Input: standard input
Output: standard output
Time Limit: 1 second
Theorem
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
|
Problem setter: Monirul Hasan, Member of Elite Problemsetters' Panel
题意很简单,就是给你一个x一个k,求出x/k的向上取整和向下取整的结果a,b然后求出任意一组p,q满足x==pa+qb;这里直接用扩展欧几里得就可以得出答案,所以这其实就是一个模板题。
上代码:
1 #include <stdio.h> 2 #include <math.h> 3 #define LL long long 4 #define I64 ll 5 using namespace std; 6 7 LL gcd(LL a,LL b) 8 { 9 return b==0 ? a : gcd(b,a%b); 10 } 11 12 void ex_gcd(LL a,LL b,LL &x,LL &y) 13 { 14 if(b==0) {x=1;y=0;return ;} 15 ex_gcd(b,a%b,x,y); 16 LL t=y; 17 y=x-(a/b)*y; 18 x=t; 19 } 20 21 int main() 22 { 23 int t; 24 LL x,k,p,q,a,b,g; 25 //freopen("data.txt","r",stdin); 26 scanf("%d",&t); 27 while(t--) 28 { 29 scanf("%I64d %I64d",&x,&k); 30 a=floor(x*1.0/k); 31 b=ceil(x*1.0/k); 32 g=gcd(a,b); 33 a/=g; 34 b/=g; 35 x/=g; 36 ex_gcd(a,b,p,q); 37 printf("%I64d %I64d\n",p*x,q*x); 38 } 39 return 0; 40 }