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 xbottles of Ber-Cola and y Bars bars and x·a + y·b = n or tell that it's impossible.
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.
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.
7 2 3
YES 2 1
100 25 10
YES 0 10
15 4 8
NO
9960594 2551 2557
YES 1951 1949
In first example Vasya can buy two bottles of Ber-Cola and one Bars bar. He will spend exactly 2·2 + 1·3 = 7 burles.
In second example Vasya can spend exactly n burles multiple ways:
- buy two bottles of Ber-Cola and five Bars bars;
- buy four bottles of Ber-Cola and don't buy Bars bars;
- don't buy Ber-Cola and buy 10 Bars bars.
In third example it's impossible to but Ber-Cola and Bars bars in order to spend exactly nburles.
题意:一个人他有n个伯勒斯(其他国家的货币单位可以看成RMB),一罐可乐花费a伯勒斯,一罐酒( Bars bar不知道这是啥
,翻译都翻不出来)花费b伯勒斯。问是否可以买整数的可乐或者酒刚好用完n伯勒斯。其中可乐或者酒数量可以其中一个为0. 可以输出YES,然后输出每个的数量,否则输出NO。
题解:因为n,a,b数据都比较大,不能暴力解决,最开始我先用n/a,n/b算出大概需要a,b的个数,然后两层for循环,过了,过了一会,呗hack了!
被hack代码:
#include<bits/stdc++.h>
using namespace std;
const int N=1e7;
int main()
{
int n,a,b,x,y;
while(cin>>n>>a>>b)
{
bool flag=0;
int x=n/a;
int y=n/b;
for(int i=0; i<x; i++)
{
for(int j=0; j<y; j++)
{
if(a*i+j*b==n)
{
flag=1;
x=i;
y=j;
break;
}
}
if(flag)
break;
}
if(!flag)
puts("NO");
else
{
puts("YES");
cout<<x<<" "<<y<<endl;
}
}
return 0;
}
看别人的AC代码:自己的代码能力还不行。。
#include<bits/stdc++.h>
using namespace std;
const int N=1e7;
int main()
{
int n,a,b,x,y;
cin>>n>>a>>b;
for(int i=0; i*a<=n; i++)
{
if((n-i*a)%b==0)
{
puts("YES");
cout<<i<<" "<<(n-i*a)/b<<endl;
return 0;
}
}
puts("NO");
return 0;
}