10673 - Play with Floor and Ceil

基础的二元一次不定方程, 直接扩展gcd.

View Code
 1 #include<cstdio>
 2 #include<cmath>
 3 #include<iostream>
 4 using namespace std;
 5 
 6 typedef long long ll;
 7 
 8 void gcd(ll a, ll b, ll& d, ll& x, ll&y)
 9 {
10     if(!b)
11     {
12         d = a; x = 1; y = 0;
13     }
14     else
15     {
16         gcd(b, a % b, d, y, x);
17         y -= (a / b) * x;
18     }
19 }
20 
21 int main()
22 {
23     int T;
24     cin >> T;
25     while(T--)
26     {
27         ll x, k;
28         cin >> x >> k;
29         ll a = floor(1.0 * x / k);
30         ll b = ceil(1.0 * x / k);
31         ll d, xx, yy;
32         gcd(a, b, d, xx, yy);
33         xx *= x / d;
34         yy *= x / d;
35         cout << xx << " " << yy << endl;
36     }
37     return 0;
38 }

 

posted @ 2013-01-29 19:42  ACSeed  Views(66)  Comments(0)    收藏  举报