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