您的位置:首页 > 其它

uva 10673(扩展欧几里德)

2014-03-26 23:12 253 查看
题意:

對任何2個整數 x 和 k,存在另2個整數 p 和 q 使得:

/**************************************************
* Author     : xiaohao Z
* Blog     : http://www.cnblogs.com/shu-xiaohao/ * Last modified : 2014-03-26 22:53
* Filename     : uva_10673.cpp
* Description     :
* ************************************************/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define MP(a, b) make_pair(a, b)
#define PB(a) push_back(a)

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<unsigned int,unsigned int> puu;
typedef pair<int, double> pid;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;

const int INF = 0x3f3f3f3f;
const double eps = 1E-6;

ll exgcd(ll a, ll b, ll &x, ll &y){
ll t, m;
if(!b && !a) return -1;
if(!b) x = 1, y = 0;
else {
m = exgcd(b, a%b, x, y);
t = x, x = y, y = t - (a/b)*y;
}
return m;
}

int main()
{
//    freopen("in.txt", "r", stdin);

int T;
cin >> T;
while(T--){
double a, b;
cin >> a >> b;
ll c = (ll)floor(a/b), d = (ll)ceil(a/b);
ll x, y;
exgcd(c, d, x, y);
ll oa = x*(a/__gcd(c, d)), ob = y*(a/__gcd(c, d));
cout << oa << ' ' << ob << endl;
}
return 0;
}


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