zoukankan      html  css  js  c++  java
  • 【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
    ", 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.
    ", (int)clock());
    118     #endif
    119     
    120     return 0;
    121 }
  • 相关阅读:
    bzoj2809 [Apio2012]dispatching
    bzoj2743[HEOI2012]采花
    bzoj3626[LNOI2014]LCA
    bzoj2038 [2009国家集训队]小Z的袜子(hose)——莫队
    bzoj2442[Usaco2011 Open]修剪草坪——单调队列优化
    bzoj1588[HNOI2002]营业额统计——双向链表
    洛谷1527(bzoj2738)矩阵乘法——二维树状数组+整体二分
    bzoj1503[NOI2004]郁闷的出纳员——Splay
    洛谷P2014——选课
    洛谷P1352——动规
  • 原文地址:https://www.cnblogs.com/bombe1013/p/5111059.html
Copyright © 2011-2022 走看看