UVA10673 - 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 uskeep our task simple, please make sure that the values,andfit 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

Special Thanks: Shahriar Manzoor, Member of Elite Problemsetters' Panel

题目大意:对于任意两个整数x,k,存在两个整数p和q满足:

                                            

输入x,k,找出符合要求的整数p和q,如果有多组,输出任意一组即可。

题解:赤果果的扩展欧几里得。。。。不过我提交几次都是WA,改成long就AC了,题目严重误导人啊!!!!坑死了。。。。

View Code
 1 #include<stdio.h>

 2 #include<math.h>

 3 typedef struct

 4 {

 5     long d;

 6     long x;

 7     long y;

 8 }NODE;

 9 NODE gcd(long a,long b)

10 {

11     NODE s,p;

12     if(!b)

13     {

14         s.x=1;

15         s.y=0;

16         s.d=a;

17         return s;

18     }

19     s=gcd(b,a%b);

20     p.x=s.x;

21     s.x=s.y;

22     s.y=p.x-(a/b)*s.y;

23     return s;

24 }

25 int main(void)

26 {

27     long x,k,a,b;

28     int t;

29     NODE s;

30     scanf("%d",&t);

31     while(t--)

32     {

33         scanf("%ld%ld",&x,&k);

34         a=floor((double)x/k);

35         b=ceil((double)x/k);

36         s=gcd(a,b);

37         printf("%ld %ld\n",s.x*(x/s.d),s.y*(x/s.d));

38 

39     }

40     return 0;

41 }

 

 

 

你可能感兴趣的:(with)