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 ...
随机推荐
- tomcat官方下载连接——安装版&绿色版
Tomcat绿色版Windows64位9.0.10 http://mirror.bit.edu.cn/apache/tomcat/tomcat-9/v9.0.10/bin/apache-tomcat- ...
- python测试函数的使用时间
1. 使用装饰器来衡量函数执行时间 有一个简单方法,那就是定义一个装饰器来测量函数的执行时间,并输出结果:(代码通用3.x) import time from functools import wra ...
- ASCII_01
1.来自“http://baike.baidu.com/link?url=WgFPtGe-rT6x6X0r_OiHGVZAV87Fu4_P5fvr7FsGyrm8QqTGuvVUfg4Jx7Rn-Le ...
- JDK_环境变量
1. 在系统环境变量中设置: ClASSPATH中输入: ".;C:\Program Files\Java\jdk1.7.0_07\jre\lib\rt.jar;"//java的安 ...
- spring: @RequestMapping注解
处理GET/POST请求方法 1.常用的: import org.springframework.web.bind.annotation.RequestMapping; @Controller pub ...
- jQuery: Redirect to other URL
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> ...
- Django进阶Admin篇 - admin基本配置
django admin 是django自带的一个后台app,提供了后台的管理功能. 基础知识点: 一.认识ModelAdmin 管理界面的定制类,如需扩展特定的model界面,需要从该类继承 二.注 ...
- python中zip()函数的用法
一. 定义 zip()函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的对象,这样做的好处是节约了不少内存 如果各个迭代器的元素个数不一致,则返回列表长度与最 ...
- WAF:web应用防火墙
1,sql注入2,xss3,不安全下载 code_backup.tar.gz .sql 4.隐私文件访问 .svn .git 5.弱口令6. 非授权访问 redis 7.cc攻击 性能cc攻击8.DD ...
- MySQL数据库维护、备份、和复制
预防性维护的基本原则:1)启动MySQL服务器提供的自动恢复功能2)有计划的开展预防心维护工作,定期对表进行检查,日常的表检查有助于及时发现各种小问题,并在问题变得更糟之前将其纠正.3)建立数据库备份 ...