您的位置:首页 > 其它

uva 10673 Play with Floor and Ceil

2013-09-16 22:23 531 查看
解题思路:扩展 gcd 或者 直接判断 x%k 是否等于 0, 若是,可取 p=0, q=k; 否则,可取 p=-x, q=x.

///////////////////////////////////////////////////////////////////////////
//problem_id: uva 10673
//user_id: SCNU20102200088
///////////////////////////////////////////////////////////////////////////

#include <algorithm>
#include <iostream>
#include <iterator>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
using namespace std;

///////////////////////////////////////////////////////////////////////////
#pragma comment(linker,"/STACK:1024000000,1024000000")

#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
///////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////
const double EPS=1e-9;
const double PI=acos(-1.0);
const double E=2.7182818284590452353602874713526;

const int x4[]={-1,0,1,0};
const int y4[]={0,1,0,-1};
const int x8[]={-1,-1,0,1,1,1,0,-1};
const int y8[]={0,1,1,1,0,-1,-1,-1};
///////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////
typedef long long LL;

typedef int T;
T max(T a,T b){ return a>b? a:b; }
T min(T a,T b){ return a<b? a:b; }
T gcd(T a,T b){ return b==0? a:gcd(b,a%b); }
T lcm(T a,T b){ return a/gcd(a,b)*b; }
///////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////
//Add Code:
LL exgcd(LL a,LL b,LL &d,LL &x,LL &y){
if(b==0) d=a,x=1,y=0;
else{
exgcd(b,a%b,d,y,x);
y-=x*(a/b);
}
}
///////////////////////////////////////////////////////////////////////////

int main(){
///////////////////////////////////////////////////////////////////////
//Add Code:
int Case;
scanf("%d",&Case);
while(Case--){
LL x,k,a,b,d,p,q;
scanf("%lld%lld",&x,&k);
if(x%k==0) a=x/k,b=a;
else a=x/k,b=a+1;
exgcd(a,b,d,p,q);
printf("%lld %lld\n",x/d*p,x/d*q);
}
///////////////////////////////////////////////////////////////////////
return 0;
}

///////////////////////////////////////////////////////////////////////////
/*
Testcase:
Input:
3
5 2
40 2
24444 6
Output:
1 1
1 1
0 6
*/
///////////////////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////////////////
//problem_id: uva 10673
//user_id: SCNU20102200088
///////////////////////////////////////////////////////////////////////////

#include <algorithm>
#include <iostream>
#include <iterator>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
using namespace std;

///////////////////////////////////////////////////////////////////////////
#pragma comment(linker,"/STACK:1024000000,1024000000")

#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
///////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////
const double EPS=1e-9;
const double PI=acos(-1.0);
const double E=2.7182818284590452353602874713526;

const int x4[]={-1,0,1,0};
const int y4[]={0,1,0,-1};
const int x8[]={-1,-1,0,1,1,1,0,-1};
const int y8[]={0,1,1,1,0,-1,-1,-1};
///////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////
typedef long long LL;

typedef int T;
T max(T a,T b){ return a>b? a:b; }
T min(T a,T b){ return a<b? a:b; }
T gcd(T a,T b){ return b==0? a:gcd(b,a%b); }
T lcm(T a,T b){ return a/gcd(a,b)*b; }
///////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////
//Add Code:
///////////////////////////////////////////////////////////////////////////

int main(){
///////////////////////////////////////////////////////////////////////
//Add Code:
int Case;
scanf("%d",&Case);
while(Case--){
LL x,k,p,q;
scanf("%lld%lld",&x,&k);
if(x%k==0) p=0,q=k;
else p=-x,q=x;
printf("%lld %lld\n",p,q);
}
///////////////////////////////////////////////////////////////////////
return 0;
}

///////////////////////////////////////////////////////////////////////////
/*
Testcase:
Input:
3
5 2
40 2
24444 6
Output:
1 1
1 1
0 6
*/
///////////////////////////////////////////////////////////////////////////
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: