【HDOJ】1356 The Balance

扩展欧几里得的应用。

  1 /* 1356 */
  2 #include <iostream>
  3 #include <sstream>
  4 #include <string>
  5 #include <map>
  6 #include <queue>
  7 #include <set>
  8 #include <stack>
  9 #include <vector>
 10 #include <deque>
 11 #include <bitset>
 12 #include <algorithm>
 13 #include <cstdio>
 14 #include <cmath>
 15 #include <ctime>
 16 #include <cstring>
 17 #include <climits>
 18 #include <cctype>
 19 #include <cassert>
 20 #include <functional>
 21 #include <iterator>
 22 #include <iomanip>
 23 using namespace std;
 24 //#pragma comment(linker,"/STACK:102400000,1024000")
 25 
 26 #define sti                set<int>
 27 #define stpii            set<pair<int, int> >
 28 #define mpii            map<int,int>
 29 #define vi                vector<int>
 30 #define pii                pair<int,int>
 31 #define vpii            vector<pair<int,int> >
 32 #define rep(i, a, n)     for (int i=a;i<n;++i)
 33 #define per(i, a, n)     for (int i=n-1;i>=a;--i)
 34 #define clr                clear
 35 #define pb                 push_back
 36 #define mp                 make_pair
 37 #define fir                first
 38 #define sec                second
 39 #define all(x)             (x).begin(),(x).end()
 40 #define SZ(x)             ((int)(x).size())
 41 #define lson            l, mid, rt<<1
 42 #define rson            mid+1, r, rt<<1|1
 43 
 44 int a, b, d;
 45 
 46 int e_gcd(int n, int m, int& x, int& y) {
 47     if (m == 0) {
 48         x = 1;
 49         y = 0;
 50         return n;
 51     }
 52     
 53     int ret = e_gcd(m, n%m, y, x);
 54     y -= n/m*x;
 55     
 56     return ret;
 57 }
 58 
 59 void solve() {
 60     int x, y;
 61     bool flag = false;
 62     
 63     if (a < b) {
 64         swap(a, b);
 65         flag = true;
 66     }
 67     
 68     int g = e_gcd(a, b, x, y);
 69     int mn = 1e9;
 70     int mn_ = 1e9;
 71     a /= g;
 72     b /= g;
 73     x = d/g*x;
 74     y = d/g*y;
 75     int Beg = -x/b-1;
 76     int End = y/a+1;
 77     int xx, yy;
 78     int tmp, tmp_;
 79     int ansx = 0, ansy = 0;
 80     
 81     rep(i, Beg, End+1) {
 82         xx = abs(x+b*i);
 83         yy = abs(y-a*i);
 84         tmp = xx + yy;
 85         tmp_ = a*xx + b*yy;
 86         if (tmp < mn) {
 87             mn = tmp;
 88             mn_ = tmp_;
 89             ansx = xx;
 90             ansy = yy;
 91         } else if (tmp==mn && tmp_<mn_) {
 92             mn_ = tmp_;
 93             ansx = xx;
 94             ansy = yy;
 95         }
 96     }
 97     
 98     if (flag)
 99         swap(ansx, ansy);
100     printf("%d %d\n", ansx, ansy);
101 }
102 
103 int main() {
104     ios::sync_with_stdio(false);
105     #ifndef ONLINE_JUDGE
106         freopen("data.in", "r", stdin);
107         freopen("data.out", "w", stdout);
108     #endif
109     
110     while (scanf("%d %d %d",&a,&b,&d)!=EOF) {
111         if(a==0 && b==0 && d==0)
112             break;
113         solve();
114     }
115     
116     #ifndef ONLINE_JUDGE
117         printf("time = %d.\n", (int)clock());
118     #endif
119     
120     return 0;
121 }

 

posted on 2016-01-07 20:09  Bombe  阅读(171)  评论(0编辑  收藏  举报

导航