codeforces #451 div2 B. Proper Nutrition(拓展欧几里得模板题)

B. Proper Nutrition

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Vasya has n burles. One bottle of Ber-Cola costs a burles and one Bars bar costs b burles. He can buy any non-negative integer number of bottles of Ber-Cola and any non-negative integer number of Bars bars.

Find out if it's possible to buy some amount of bottles of Ber-Cola and Bars bars and spend exactly n burles.

In other words, you should find two non-negative integers x and y such that Vasya can buy x bottles of Ber-Cola and y Bars bars and x·a + y·b = n or tell that it's impossible.

Input

First line contains single integer n (1 ≤ n ≤ 10 000 000) — amount of money, that Vasya has.

Second line contains single integer a (1 ≤ a ≤ 10 000 000) — cost of one bottle of Ber-Cola.

Third line contains single integer b (1 ≤ b ≤ 10 000 000) — cost of one Bars bar.

Output

If Vasya can't buy Bars and Ber-Cola in such a way to spend exactly n burles print «NO» (without quotes).

Otherwise in first line print «YES» (without quotes). In second line print two non-negative integers x and y — number of bottles of Ber-Cola and number of Bars bars Vasya should buy in order to spend exactly n burles, i.e. x·a + y·b = n. If there are multiple answers print any of them.

Any of numbers x and y can be equal 0.

 

大意就是给你a,b,n,求是否有一组非负整数(x,y)满足 ax+by=n

直接附上代码 注释在其中

 

 

 

#include<bits/stdc++.h>
using namespace std;
void exgcd(long long int a,long long int b,long long int& x,long long int& y){
    if(!b){
        x=1;y=0;
    }
    else{
        exgcd(b,a%b,y,x);
        y-=x*(a/b);
    }
}
//int gcd(long long int a,long long int b){
//    return b==0?a:gcd(b,a%b);
//}
int main(){
    long long int n,a,b,x,y,g;
    while(cin>>n>>a>>b){
        g=__gcd(a,b);   // __gcd(a,b) 头文件 algorithm 
        if(n%g!=0)
        {            //  对AX+BY=C 若x,y为整数 (AX+BY)/GCD(A,B)也为整数,所以C/GCD(A,B)也一
                     //  定要是整数,如果要求整数解的话。 
            cout<<"NO"<<endl; 
        } 
        else{
            n/=g,a/=g,b/=g; //化简之后 gcd(a,b)=1 
        exgcd(a,b,x,y);
        x=x*n;    //其实是 x=x*n/1; 
        y=y*n;
        x = (x%b + b)%b;   //x可能为负数所以加上b (x % b > -b ) 这样之后x是满足条件的最小正数 
        y = (n - a*x)/b;       //y一定是整数  因为上式x实质是x+n*b1  此式子y实质是 y-n*a1 x越大y越小 当x为最小正数时 y取x满足条件的最大值 
        if(y>=0){
            cout<<"YES"<<endl; 
        cout<<x<<" "<<y<<endl;}
        else
        cout<<"NO"<<endl;}
    }
    return 0;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值