一道扩展欧几里得题目
不过这次需要求一组满足条件的解,而不是求出第一组解就行了;
方法是求出第一组解后,再求出通解的表达式,然后再将表达式看成
t的一次函数,由题目的要求求出t的范围;然后就是分情况讨论求一次函数的最大值了。。。。。
代码如下:
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
#define LL long long
void exgcd(LL a,LL b,LL &d,LL &x,LL &y)
{
if(!b)
{
x=1;
y=0;
d=a;
}
else
{
exgcd(b,a%b,d,y,x);
y-=(a/b)*x;
}
}
int main()
{
LL c1,c2,n1,n2,x,y,d,n,t,k,cost,x1,y1;
while(cin>>n&&n)
{
cin>>c1>>n1;
cin>>c2>>n2;
exgcd(n1,n2,d,x,y);
LL k=n/d;
if(n%d)
{
cout<<"failed"<<endl;
}
else
{
LL tmin=ceil(-n*x/(double)n2); //由x1>=0,y1>=0求出t的取值范围
LL tmax=floor(n*y/(double)n1);
// cout<<tmin<<" "<<tmax<<endl;
if(tmin>tmax)
cout<<"failed"<<endl;
else if(c1 * n2 <= c2 * n1)
{
x=k*x+n2/d*tmax;
y=k*y-n1/d*tmax;
cout<<x<<" "<<y<<endl;
}
else
{
x=k*x+n2/d*tmin;
y=k*y-n1/d*tmin;
cout<<x<<" "<<y<<endl;
}
}
}
return 0;
}