uva 10673 - Play with Floor and Ceil

扩展欧几里德算法:找出一对整数(x, y),是的ax + by = gcd(a, b),哈哈,直接套

#include <iostream> #include <cmath> using namespace std; long long x, k; long long lx, ly; void gcd(long long a, long long b, long long &d, long long &p, long long &q) { if(b == 0) { d = a; p = 1; q = 0; }else { //这算法最最最要注意这里的p,q要调换顺序 gcd(b, a % b, d, q, p); q -= p * (a / b); } } //#define TEST int main() { #ifdef TEST freopen("input.txt", "r", stdin); #endif int t; cin >> t; long long p, q, d; while(t--) { cin >> x >> k; lx = floor((double)x / (double)k); ly = ceil((double)x / (double)k); gcd(lx, ly, d, p, q); d = x / d; cout << p * d << " " << q * d << endl; } return 0; } 

你可能感兴趣的:(uva 10673 - Play with Floor and Ceil)