扩展欧几里得的应用。

 /* 1356 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <bitset>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 int a, b, d; int e_gcd(int n, int m, int& x, int& y) {
if (m == ) {
x = ;
y = ;
return n;
} int ret = e_gcd(m, n%m, y, x);
y -= n/m*x; return ret;
} void solve() {
int x, y;
bool flag = false; if (a < b) {
swap(a, b);
flag = true;
} int g = e_gcd(a, b, x, y);
int mn = 1e9;
int mn_ = 1e9;
a /= g;
b /= g;
x = d/g*x;
y = d/g*y;
int Beg = -x/b-;
int End = y/a+;
int xx, yy;
int tmp, tmp_;
int ansx = , ansy = ; rep(i, Beg, End+) {
xx = abs(x+b*i);
yy = abs(y-a*i);
tmp = xx + yy;
tmp_ = a*xx + b*yy;
if (tmp < mn) {
mn = tmp;
mn_ = tmp_;
ansx = xx;
ansy = yy;
} else if (tmp==mn && tmp_<mn_) {
mn_ = tmp_;
ansx = xx;
ansy = yy;
}
} if (flag)
swap(ansx, ansy);
printf("%d %d\n", ansx, ansy);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif while (scanf("%d %d %d",&a,&b,&d)!=EOF) {
if(a== && b== && d==)
break;
solve();
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

【HDOJ】1356 The Balance的更多相关文章

  1. 【HDOJ】1709 The Balance

    母函数,指数可以为1也可以为-1,扩大指数加消减发现TLE,于是采用绝对值就过了. #include <stdio.h> #include <string.h> #define ...

  2. 【POJ】2142 The Balance 数论(扩展欧几里得算法)

    [题意]给定a,b,c,在天平左边放置若干重量a的砝码,在天平右边放置若干重量b的砝码,使得天平两端砝码差为c.设放置x个A砝码和y个B砝码,求x+y的最小值. [算法]数论(扩展欧几里德算法) [题 ...

  3. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  4. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

  5. 【HDOJ】【3516】Tree Construction

    DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...

  6. 【HDOJ】【3480】Division

    DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...

  7. 【HDOJ】【2829】Lawrence

    DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...

  8. 【HDOJ】【3415】Max Sum of Max-K-sub-sequence

    DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...

  9. 【HDOJ】【3530】Subsequence

    DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...

随机推荐

  1. Kafka-0.10.0.0入门

    搭建环境略(伪集群即可以),但要注意Kafka的配置必须配置的,少配了也一样可以用,但是只能单机使用,外部机器无法连接,网上也有说. host.name=192.168.1.30 advertised ...

  2. java继承实例。

    定义了一个点类point,然后线条类line继承了point类,正方形类Suare继承point类. package test; import javax.swing.*; public class ...

  3. 构造函数为什么不能是虚函数 ( 转载自C/C++程序员之家)

    从存储空间角度,虚函数对应一个指向vtable虚函数表的指针,这大家都知道,可是这个指向vtable的指针其实是存储在对象的内存空间的.问题出来了,如果构造函数是虚的,就需要通过 vtable来调用, ...

  4. Skia

    1 What is SKIA. Skia is an open source 2D graphics library which provides common APIs that work acro ...

  5. POJ 1080 Human Gene Functions -- 动态规划(最长公共子序列)

    题目地址:http://poj.org/problem?id=1080 Description It is well known that a human gene can be considered ...

  6. HTML5之字体

    - 使用CSS样式来定义 context.font = [CSS font property] context.font = [font-style font-variant font-weight ...

  7. redis 常用操作命令

    操作相关的命令连接 quit:关闭连接(connection)auth:简单密码认证 持久化 save:将数据同步保存到磁盘bgsave:将数据异步保存到磁盘lastsave:返回上次成功将数据保存到 ...

  8. Linux查找软件的安装路径

    软件安装的路径可能不止一个,可以使用whereis命令查看软件安装的所有路径,以mysql为例: whereis mysql 该命令会返回软件的所有安装路径: mysql: /usr/bin/mysq ...

  9. #LeetCode# Container With Most Water (todo)

    描述: 实现1 -- 求所有可能的值,O(N^2),超时了(因为超时没有跑所有的测试用例,所以不确定还有没有其他问题) 代码: def maxArea(self, height): tmp = len ...

  10. PL/SQL — BULK COLLECT用法

    BULK COLLECT 子句会批量检索结果,即一次性将结果集绑定到一个集合变量中,并从SQL引擎发送到PL/SQL引擎.通常可以在SELECT INTO.FETCH INTO以及RETURNING ...