扩展欧几里得 Poj2142
2013-08-27 05:55
363 查看
Poj2142
The Balance
Description
Ms. Iyo Kiffa-Australis has a balance and only two kinds of weights to measure a dose of medicine. For example, to measure 200mg of aspirin using 300mg weights and 700mg weights, she can put one 700mg weight on the side of the medicine and three 300mg weights
on the opposite side (Figure 1). Although she could put four 300mg weights on the medicine side and two 700mg weights on the other (Figure 2), she would not choose this solution because it is less convenient to use more weights.
You are asked to help her by calculating how many weights are required.
Input
The input is a sequence of datasets. A dataset is a line containing three positive integers a, b, and d separated by a space. The following relations hold: a != b, a <= 10000, b <= 10000, and d <= 50000. You may assume that it is possible to measure d mg using
a combination of a mg and b mg weights. In other words, you need not consider "no solution" cases.
The end of the input is indicated by a line containing three zeros separated by a space. It is not a dataset.
Output
The output should be composed of lines, each corresponding to an input dataset (a, b, d). An output line should contain two nonnegative integers x and y separated by a space. They should satisfy the following three conditions.
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.
大概意思 :给定 a b k找到满足ax+by=k的令|x|+|y|最小(相等时
令a|x|+b|y|最小)[思考:为什么一个式子就可以表示题目中的 3种情况]
用(X%B+B)%B 就可以求出最小非负整数解x了,然后比较附近 两个点。 同理,(Y%A+A)%A; 就可以求出最小非负整数解y。
总共比较6个点。
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
void gcd(__int64 a,__int64 b,__int64& d,__int64& x,__int64& y)
{
if(!b) {d=a; x=1; y=0;}
else {gcd(b,a%b,d,y,x); y-=x*(a/b); }
}
//方程 ax+by=c 的整数解
int main()
{
__int64 i,j,x,y,a,b,c;
__int64 x1,y1,x0,y0,X,Y,t,g,B,A,ansx,ansy,sum1,sum2;
while(~scanf("%I64d%I64d%I64d",&a,&b,&c))
{
if(a+b+c==0) break;
gcd(a,b,g,x0,y0); //g=gcd(a,b)
t=c/g; // t是系数
X=x0*t; Y=y0*t;
B=b/g; A=a/g;
sum1=sum2=9999999999;
x1=(X%B+B)%B;
for(i=-1;i<=1;i++)
{
x=x1+i*B; y=(c-a*x)/b;
if(x<0) x=-x;
if(y<0) y=-y;
if(x+y<sum1) {ansx=x,ansy=y; sum1=x+y; }
else if(x+y==sum1)
{
if(a*x+b*y<sum2)
{ansx=x,ansy=y; sum2=a*x+b*y; }
}
}
y1=(Y%A+A)%A;
for(i=-1;i<=1;i++)
{
y=y1+i*A; x=(c-b*y)/a;
if(x<0) x=-x;
if(y<0) y=-y;
if(x+y<sum1) {ansx=x,ansy=y; sum1=x+y; }
else if(x+y==sum1)
{
if(a*x+b*y<sum2)
{ansx=x,ansy=y; sum2=a*x+b*y; }
}
}
printf("%I64d %I64d\n",ansx,ansy);
}
return 0;
}
The Balance
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 3355 | Accepted: 1478 |
Ms. Iyo Kiffa-Australis has a balance and only two kinds of weights to measure a dose of medicine. For example, to measure 200mg of aspirin using 300mg weights and 700mg weights, she can put one 700mg weight on the side of the medicine and three 300mg weights
on the opposite side (Figure 1). Although she could put four 300mg weights on the medicine side and two 700mg weights on the other (Figure 2), she would not choose this solution because it is less convenient to use more weights.
You are asked to help her by calculating how many weights are required.
Input
The input is a sequence of datasets. A dataset is a line containing three positive integers a, b, and d separated by a space. The following relations hold: a != b, a <= 10000, b <= 10000, and d <= 50000. You may assume that it is possible to measure d mg using
a combination of a mg and b mg weights. In other words, you need not consider "no solution" cases.
The end of the input is indicated by a line containing three zeros separated by a space. It is not a dataset.
Output
The output should be composed of lines, each corresponding to an input dataset (a, b, d). An output line should contain two nonnegative integers x and y separated by a space. They should satisfy the following three conditions.
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.
大概意思 :给定 a b k找到满足ax+by=k的令|x|+|y|最小(相等时
令a|x|+b|y|最小)[思考:为什么一个式子就可以表示题目中的 3种情况]
用(X%B+B)%B 就可以求出最小非负整数解x了,然后比较附近 两个点。 同理,(Y%A+A)%A; 就可以求出最小非负整数解y。
总共比较6个点。
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
void gcd(__int64 a,__int64 b,__int64& d,__int64& x,__int64& y)
{
if(!b) {d=a; x=1; y=0;}
else {gcd(b,a%b,d,y,x); y-=x*(a/b); }
}
//方程 ax+by=c 的整数解
int main()
{
__int64 i,j,x,y,a,b,c;
__int64 x1,y1,x0,y0,X,Y,t,g,B,A,ansx,ansy,sum1,sum2;
while(~scanf("%I64d%I64d%I64d",&a,&b,&c))
{
if(a+b+c==0) break;
gcd(a,b,g,x0,y0); //g=gcd(a,b)
t=c/g; // t是系数
X=x0*t; Y=y0*t;
B=b/g; A=a/g;
sum1=sum2=9999999999;
x1=(X%B+B)%B;
for(i=-1;i<=1;i++)
{
x=x1+i*B; y=(c-a*x)/b;
if(x<0) x=-x;
if(y<0) y=-y;
if(x+y<sum1) {ansx=x,ansy=y; sum1=x+y; }
else if(x+y==sum1)
{
if(a*x+b*y<sum2)
{ansx=x,ansy=y; sum2=a*x+b*y; }
}
}
y1=(Y%A+A)%A;
for(i=-1;i<=1;i++)
{
y=y1+i*A; x=(c-b*y)/a;
if(x<0) x=-x;
if(y<0) y=-y;
if(x+y<sum1) {ansx=x,ansy=y; sum1=x+y; }
else if(x+y==sum1)
{
if(a*x+b*y<sum2)
{ansx=x,ansy=y; sum2=a*x+b*y; }
}
}
printf("%I64d %I64d\n",ansx,ansy);
}
return 0;
}
相关文章推荐
- POJ - 2142 The Balance(扩展欧几里得求解不定方程)
- POJ 2142 扩展欧几里得
- POJ-2142-The Balance(扩展欧几里得)
- POJ 2142 扩展欧几里得
- POJ 2142 扩展欧几里得
- poj 2142 数论 扩展欧几里得
- POJ 2142 - The Balance [ 扩展欧几里得 ]
- POJ 2142 The Balance 扩展欧几里得,求|x|+|y|最小
- poj 2142 The Balance 数论-扩展欧几里得
- poj 2142 The Balance (基础扩展欧几里得)
- poj 2142 The Balance 扩展欧几里得
- The Balance POJ 2142 扩展欧几里得
- POJ 2142:The Balance_扩展欧几里得(多组解)
- POJ 2142 The Balance 求|x|+|y|最小(扩展欧几里得)
- [POJ 2142]The Balance(扩展欧几里得)
- poj 2142 扩展欧几里得解ax+by=c
- poj 2142 The Balance 扩展欧几里得
- POJ2142 扩展欧几里得
- POJ 2142:The Balance_扩展欧几里得
- poj 1061/2142 扩展的欧几里得(青蛙的约会/砝码称重)