【HDOJ】1356 The Balance
扩展欧几里得的应用。
/* 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的更多相关文章
- 【HDOJ】1709 The Balance
母函数,指数可以为1也可以为-1,扩大指数加消减发现TLE,于是采用绝对值就过了. #include <stdio.h> #include <string.h> #define ...
- 【POJ】2142 The Balance 数论(扩展欧几里得算法)
[题意]给定a,b,c,在天平左边放置若干重量a的砝码,在天平右边放置若干重量b的砝码,使得天平两端砝码差为c.设放置x个A砝码和y个B砝码,求x+y的最小值. [算法]数论(扩展欧几里德算法) [题 ...
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- 【HDOJ】【3506】Monkey Party
DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...
- 【HDOJ】【3516】Tree Construction
DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...
- 【HDOJ】【3480】Division
DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...
- 【HDOJ】【2829】Lawrence
DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...
- 【HDOJ】【3415】Max Sum of Max-K-sub-sequence
DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...
- 【HDOJ】【3530】Subsequence
DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...
随机推荐
- for循环例题
1· 一对幼兔一个月后长成小兔(每对兔子默认一公一母),再过一个月长成成兔并且生下一对小兔,以此类推,两年后有多少对兔子? Console.Write("输入年:"); ...
- ajaxError
$(document).ready(function () { $('input:button').click(function() { if($('#fileName').val() == '') ...
- MVC构架思想
一.构架的基本思想 采用MVC构架一个网站时,最好随时随地地将脑袋中切割成三份(M,V,C),这是一个最基本的切割单位,而且也是最容易切割的三个部分,但是在实务上,通常不会这么简单,有时候我们会再多切 ...
- NOIP200905
暴力,简单,,, 题目好长,差点没读完 试题描述 R国和S国正陷入战火之中,双方都互派间谍,潜入对方内部,伺机行动.历经艰险后,潜伏于S国的R国间谍小C终于摸清了S国军用密码的编码规则:1)S国军方内 ...
- Registry uninstall values
Original link: http://windowssucks.wordpress.com/win-registry-uninstall-values/ -------------------- ...
- 九度OJ 1547 出入栈 -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1547 题目描述: 给定一个初始为空的栈,和n个操作组成的操作序列,每个操作只可能是出栈或者入栈. 要求在操作序列的 ...
- json字符串转JSONObject,输出JSONObject问题
json架包:json-lib-2.4-jdk15.jar json字符串(存在null值)转JSONObject 后return JSONObject对象的时候会报错 例如: String str= ...
- Mac 系统下安装 MySql
Mac原生没有带MySql,想要使用需要自己去安装. 下载. 首先去mysql官网下载安装包. 由于现在mysql对企业有服务,所以有所谓社区版(community)和企业版(enterprise), ...
- Java知识总结--数据库
1 薪水排序后薪水排名在第3-5的员工 1)select * from(select ename,sal,rownum rn from (select ename,sal from emp_44 wh ...
- windows 使用excel导出的问题
解决 window server2008 r2 没有注册Ofiice组件的方法 .NET下在用Microsoft.Office.Interop.Excel及word 操作Excel和Word时, ...