uva--10090

一道扩展欧几里得题目

不过这次需要求一组满足条件的解,而不是求出第一组解就行了;

方法是求出第一组解后,再求出通解的表达式,然后再将表达式看成

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;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值