Codeforces Round #451 (Div. 2) B. Proper Nutrition【枚举/扩展欧几里得/给你n问有没有两个非负整数x,y满足x·a + y·b = n】

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 andx·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.

Examples
input
7
2
3
output
YES
2 1
input
100
25
10
output
YES
0 10
input
15
4
8
output
NO
input
9960594
2551
2557
output
YES
1951 1949
Note

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 n burles.

【题意】:给你n问有没有两个非负整数x,y满足x·a + y·b = n。

 【分析】:略带技巧的枚举。非负整数x,y上面做文章。

【代码】:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,a,b;
    cin>>n>>a>>b;
    for(int i=0;i*a<=n;++i)//自己这个地方是i<=n,错了,可能出现到负数的时候%b==0的情况
    {
        if((n-i*a)%b==0)
        {
            int j=(n-i*a)/b;
            puts("YES");
            printf("%d %d\n",i,j);
            exit(0);
        }
    }
    puts("NO");
    return 0;
}
枚举,整数性判断

 

 

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
ll ex_gcd(ll a,ll b,ll &x,ll &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
   ll r=ex_gcd(b,a%b,x,y);
    ll t=x;
    x=y;
    y=t-a/b*y;
    return r;
}
bool jie(ll a,ll b,ll c,ll &x,ll &y)
{
    ll r=ex_gcd(a,b,x,y);
    if(c%r!=0) return 0;
    ll zz=c/r;
    x*=zz;
    y*=zz;
    if(x<0&&y<0) return 0;
     ll bb=b/r;
     ll aa=a/r;
    if(x<0&&y>0)
    {
        ll t=-1*x/bb;
        if(x+bb*t<0) t++;
        x=x+bb*t;
        y=y-aa*t;
        if(y>=0) return 1;
        else return 0;
    }
   else if(x>0&&y<0)
    {
        ll t=y/aa;
        if(y-aa*t<0) t--;

        x=x+bb*t;
        y=y-aa*t;
        if(x>=0) return 1;
        else return 0;
    }
    return 1;
}
ll a,b,n,x,y;
int main()
{
    while(~scanf("%lld%lld%lld",&n,&a,&b))
    {
        x=0;
        y=0;
        if(jie(a,b,n,x,y))
        {
            printf("YES\n");
            printf("%lld %lld\n",x,y);
        }
        else
        {
            printf("NO\n");
        }
    }
    return 0;
}
拓展欧几里得

 

posted @ 2017-12-22 19:55  Roni_i  阅读(261)  评论(0编辑  收藏  举报