您的位置:首页 > 其它

uva 10673 Play with Floor and Ceil(扩展gcd)

2015-01-01 11:35 337 查看
题意:

给x, k 求:



一组p,q。

解析:

裸exgcd。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long

using namespace std;
const int maxn = 1e6;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = 4 * atan(1.0);
const double ee = exp(1.0);

void 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 -= a / b * x;
}
}

int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
int ncase;
scanf("%d", &ncase);
while (ncase--)
{
LL o, p;
scanf("%lld%lld", &o, &p);
LL a = floor((double)o / p);
LL b = ceil((double)o / p);
LL c = o;
LL x, y, d;
exgcd(a, b, d, x, y);
///LL r = b / d;
x = x / d * c;
///x = (x % r + r) % r; //求最小整数解
y = (c - a * x) / b;
printf("%lld %lld\n", x, y);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: