Codeforces Round #451 (Div. 2) - B. Proper Nutrition (扩展欧几里得)

本文介绍了解决特定形式的二元一次方程的方法,即寻找正整数解(x, y),使得 x*a + y*b = n 成立,其中 a 和 b 是已知的正整数,n 是给定的总金额。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

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.



题意:

解个二元一次方程,要正整数解。


#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <vector>
#include <map>
using namespace std;
typedef long long lint;

lint ex_gcd(lint a, lint b, lint& x, lint& y)
{
	if(b == 0)
	{
		x = 1;
		y = 0;
		return a;
	}
	lint r = ex_gcd(b, a % b, x, y);
	lint t = x;
	x = y;
	y = t - a / b * y;
	return r;
}

bool check(lint a, lint b, lint c, lint& x, lint& y)
{
	lint r = ex_gcd(a, b, x, y);
	if(c % r)
	{
		return false;
	}
	a /= r; b /= r; c /= r;
	x *= c;
	x = ((x % b) + b ) % b;
	y = (c - a * x) / b;
	return y >= 0;
}
int main()
{
	lint c,a,b;
	cin>>c>>a>>b;
	lint x, y;
	if(check(a, b, c, x, y))
	{
		printf("YES\n%lld %lld\n", x, y);
	}
	else
	{
		printf("NO\n");
	}


}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值