POJ2142(扩展欧几里得)
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 5991 | Accepted: 2605 |
Description
You are asked to help her by calculating how many weights are required.

Input
The end of the input is indicated by a line containing three zeros separated by a space. It is not a dataset.
Output
- You can measure dmg using x many amg weights and y many bmg weights.
- The total number of weights (x + y) is the smallest among those pairs of nonnegative integers satisfying the previous condition.
- The total mass of weights (ax + by) is the smallest among those pairs of nonnegative integers satisfying the previous two conditions.
No extra characters (e.g. extra spaces) should appear in the output.
Sample Input
700 300 200
500 200 300
500 200 500
275 110 330
275 110 385
648 375 4002
3 1 10000
0 0 0
Sample Output
1 3
1 1
1 0
0 3
1 1
49 74
3333 1
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
LL extgcd(LL a,LL b,LL &x,LL &y)
{
LL d=a;
if(b!=)
{
d=extgcd(b,a%b,y,x);
y-=(a/b*x);
}
else
{
x=;
y=;
}
return d;
}
LL GCD(LL a,LL b)
{
if(b==)
{
return a;
}
return GCD(b,a%b);
}
LL a,b,d;
int main()
{
while(scanf("%lld%lld%lld",&a,&b,&d)!=EOF&&(a+b+d)!=)
{
LL x,y;
LL gcd=GCD(a,b);
a/=gcd;
b/=gcd;
d/=gcd;
extgcd(a,b,x,y);
x*=d;
y*=d; LL x1=x;
x1=(x1%b+b)%b;//ax+by=1最小正整数解
LL y1=(d-a*x1)/b;
if(y1<)y1=-y1; LL y2=y; y2=(y2%a+a)%a; //最小正整数解
LL x2=(d-b*y2)/a;
if(x2<)x2=-x2; if(x1+y1<x2+y2)
{
printf("%lld %lld\n",x1,y1);
}
else
{
printf("%lld %lld\n",x2,y2);
}
} return ;
}
Java版:
import java.util.Scanner;
class BigInt{
private long x;
public BigInt(){}
public BigInt(long x)
{
this.x = x;
}
void setValue(long x)
{
this.x = x;
}
long getValue()
{
return x;
}
}
public class Main{
Scanner in = new Scanner(System.in);
long a, b, c;
long gcd(long a, long b)
{
if(b == )
{
return a;
}
return gcd(b, a % b);
}
long extgcd(long a, long b, BigInt x, BigInt y)
{
long d = a;
if(b != )
{
d = extgcd(b, a % b, y, x);
y.setValue(y.getValue() - a / b * x.getValue());
}
else
{
x.setValue();
y.setValue();
}
return d;
}
public Main()
{
while(in.hasNext())
{
a = in.nextLong();
b = in.nextLong();
c = in.nextLong();
if(a + b + c == ) break;
long gcd = gcd(a, b);
a /= gcd;
b /= gcd;
c /= gcd;
BigInt x = new BigInt(), y = new BigInt();
extgcd(a, b, x, y);
x.setValue(c * x.getValue());
y.setValue(c * y.getValue());
long x1 = x.getValue();
long y1 = y.getValue();
x1 = (x1 % b + b) % b;
y1 = (c - x1 * a) / b;
if(y1 < ) y1 = -y1;
long x2 = x.getValue();
long y2 = y.getValue();
y2 = (y2 % a + a) % a;
x2 = (c - y2 * b) / a;
if(x2 < ) x2 = -x2;
if(x1 + y1 < x2 + y2)
{
System.out.println(x1 + " " + y1);
}
else
{
System.out.println(x2 + " " + y2);
}
}
}
public static void main(String[] args){
new Main();
}
}
POJ2142(扩展欧几里得)的更多相关文章
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C.Ray Tracing (模拟或扩展欧几里得)
http://codeforces.com/contest/724/problem/C 题目大意: 在一个n*m的盒子里,从(0,0)射出一条每秒位移为(1,1)的射线,遵从反射定律,给出k个点,求射 ...
- UVA 12169 Disgruntled Judge 枚举+扩展欧几里得
题目大意:有3个整数 x[1], a, b 满足递推式x[i]=(a*x[i-1]+b)mod 10001.由这个递推式计算出了长度为2T的数列,现在要求输入x[1],x[3],......x[2T- ...
- UVA 10090 Marbles 扩展欧几里得
来源:http://www.cnblogs.com/zxhl/p/5106678.html 大致题意:给你n个球,给你两种盒子.第一种盒子每个盒子c1美元,可以恰好装n1个球:第二种盒子每个盒子c2元 ...
- POJ 1061 青蛙的约会 扩展欧几里得
扩展欧几里得模板套一下就A了,不过要注意刚好整除的时候,代码中有注释 #include <iostream> #include <cstdio> #include <cs ...
- 【64测试20161112】【Catalan数】【数论】【扩展欧几里得】【逆】
Problem: n个人(偶数)排队,排两行,每一行的身高依次递增,且第二行的人的身高大于对应的第一行的人,问有多少种方案.mod 1e9+9 Solution: 这道题由1,2,5,14 应该想到C ...
- poj 2891 扩展欧几里得迭代解同余方程组
Reference: http://www.cnblogs.com/ka200812/archive/2011/09/02/2164404.html 之前说过中国剩余定理传统解法的条件是m[i]两两互 ...
- poj 2142 扩展欧几里得解ax+by=c
原题实际上就是求方程a*x+b*y=d的一个特解,要求这个特解满足|x|+|y|最小 套模式+一点YY就行了 总结一下这类问题的解法: 对于方程ax+by=c 设tm=gcd(a,b) 先用扩展欧几里 ...
- poj 1061 扩展欧几里得解同余方程(求最小非负整数解)
题目可以转化成求关于t的同余方程的最小非负数解: x+m*t≡y+n*t (mod L) 该方程又可以转化成: k*L+(n-m)*t=x-y 利用扩展欧几里得可以解决这个问题: eg:对于方程ax+ ...
- Codeforces7C 扩展欧几里得
Line Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Status ...
随机推荐
- eclipse官网下载
Provided by IBM Cloud Eclipse IDE for Java Developers http://eclipse.bluemix.net/packages/photon/dat ...
- painting fence - 分治 - Codeforces 448c
2017-08-02 14:27:18 writer:pprp 题意: • 每块木板宽度均为1,高度为h[i] • n块木板连接为宽度为n的栅栏 • 每次可以刷一横或一竖(上色) • 最少刷多少次可以 ...
- 数字组合问题:Combination,CombinationSum,CombinationSum2,CombinationSum3
Combination问题描述:给定n和k,找出1-n之间所有k个数的组合,例如:n=3,k=2,返回 [[1,2] [1,3] [2,3]] 算法分析:利用递归.递归边界就是curr.size( ...
- php超级全局变量和魔术变量
php超级全局变量和魔术变量 一.总结 一句话总结: 1.两者的书写形式非常不一样,超级全局变量是$_大写变量名 的形式,魔术变量是 __大写变量名的形式__ 2.两者的应用范围不一样,超级全局变量是 ...
- ffmpeg的centos、msys2、msvc编译
msys2 和 centos https://ffmpeg.org/download.html https://ffmpeg.zeranoe.com/builds/ Windows MSYS2准备 1 ...
- 四十一 Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)基本的索引和文档CRUD操作、增、删、改、查
elasticsearch(搜索引擎)基本的索引和文档CRUD操作 也就是基本的索引和文档.增.删.改.查.操作 注意:以下操作都是在kibana里操作的 elasticsearch(搜索引擎)都是基 ...
- elasticsearch 2.2+ index.codec: best_compression启用压缩
官方说法,来自https://www.elastic.co/guide/en/elasticsearch/reference/2.2/index-modules.html#_static_index_ ...
- iOS自动化探索(六)自动化测试框架pytest - fixtures
Fixture介绍 fixture是pytest特有的功能,它用pytest.fixture标识,定义在函数前面.在编写测试函数的时候,可以将此函数名称做为传入参数,pytest将会以依赖注入方式,将 ...
- HTML——部分MP4在谷歌浏览器上无法播放
Chrome浏览器支持HTML5,它支持原生播放部分的MP4格式(不用通过Flash等插件). 为什么是部分MP4呢?MP4有非常复杂的含义(见http://en.wikipedia.org/wiki ...
- JAM计数法(模拟)
题目描述 Jam是个喜欢标新立异的科学怪人.他不使用阿拉伯数字计数,而是使用小写英文字母计数,他觉得这样做,会使世界更加丰富多彩.在他的计数法中,每个数字的位数都是相同的(使用相同个数的字母),英文字 ...