您的位置:首页 > 其它

UVA 10673 Play with Floor and Ceil

2013-10-27 11:58 357 查看
 


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

 

题意:给你一个x一个k,求出x/k的向上取整和向下取整的结果a,b然后求出任意一组p,q满足x==pa+qb;

这是一个模板题。

这里直接用扩展欧几里德就可以。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;

void gcd(long a,long b,long &d,long &x,long &y)
{
if(!b)
{
d=a;
x=1;
y=0;
}
else
{
gcd(b,a%b,d,y,x);
y=y-x*(a/b);
}
}

int main()
{
int T;
scanf("%d",&T);
while(T--)
{
long a,b,c,d,k,x,y;
scanf("%ld%ld",&c,&k);
a=floor(1.0*c/k);
b=ceil(1.0*c/k);
gcd(a,b,d,x,y);
x*=c/d;
y*=c/d;
printf("%ld %ld\n",x,y);
}
return 0;
}


 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  扩展欧几里得