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
Special Thanks: Shahriar Manzoor, Member of Elite Problemsetters' Panel
/*如果你做了POJ 1061青蛙的约会,这道题很简单*/
#include<iostream>
#include<cmath>
using namespace std;
long long int x,k,t,p,q;
int main()
{
void ex_gcd(long long int a,long long int b,long long int& d,long long int& x,long long int& y);
long long int a,b,c,d,x0,y0,r;
cin>>t;
while(t--)
{
cin>>x>>k;
a=floor((double)x/k);//向下取整
b=ceil((double)x/k);//向上取整
c=x;
ex_gcd(a,b,d,x0,y0);
r=b/d;
p=c/d*x0;/*此步是求特解t*/
q=(x-a*p)/b;
cout<<p<<" "<<q<<endl;
}
return 0;
}
/*已知整数a、b,扩展欧几里得算法可以在求得a、b的最大公约数的同时,
能找到整数x、y(其中一个很可能是负数),
使它们满足等式ax + by = gcd(a, b).*/
void ex_gcd(long long int a,long long int b,long long int& d,long long int& x,long long int& y)
{
if(b==0){d=a;x=1;y=0;}
else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}
}