cf1214A A. Optimal Currency Exchange

										A. Optimal Currency Exchange
										time limit per test1.5 seconds
										memory limit per test512 megabytes
										inputstandard input
										outputstandard output

Andrew was very excited to participate in Olympiad of Metropolises. Days flew by quickly, and Andrew is already at the airport, ready to go home. He has n rubles left, and would like to exchange them to euro and dollar bills. Andrew can mix dollar bills and euro bills in whatever way he wants. The price of one dollar is d rubles, and one euro costs e rubles.

Recall that there exist the following dollar bills: 1, 2, 5, 10, 20, 50, 100, and the following euro bills — 5, 10, 20, 50, 100, 200 (note that, in this problem we do not consider the 500 euro bill, it is hard to find such bills in the currency exchange points). Andrew can buy any combination of bills, and his goal is to minimize the total number of rubles he will have after the exchange.

Help him — write a program that given integers n, e and d, finds the minimum number of rubles Andrew can get after buying dollar and euro bills.

Input
The first line of the input contains one integer n (1≤n≤108) — the initial sum in rubles Andrew has.

The second line of the input contains one integer d (30≤d≤100) — the price of one dollar in rubles.

The third line of the input contains integer e (30≤e≤100) — the price of one euro in rubles.

Output
Output one integer — the minimum number of rubles Andrew can have after buying dollar and euro bills optimally.

Examples
input

100
60
70
output
40
input
410
55
70
output
5
input
600
60
70
output
0
Note
In the first example, we can buy just 1 dollar because there is no 1 euro bill.

In the second example, optimal exchange is to buy 5 euro and 1 dollar.

In the third example, optimal exchange is to buy 10 dollars in one bill.
題意: 給出n盧布,並對應給出一美元可兌換的盧布金額和一歐元可兌換的盧布金額,同時給出了不同的美元面額和歐元面額,問兌換後(可以任意兌換,或同時兌換美元和歐元), 問最少剩下的盧布值爲多少。
思路: 給出的美元面額中,除1以外,其餘均爲1的倍數;給出的歐元面額中,除5外,其餘均爲5的倍數,所以不管如何兌換,我們只考慮最小面額,先對一歐元能兌換的金額不斷進行累加枚舉,再不斷一美元能兌換的金額取模更新最小值。詳情看代碼。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
	int n, d, e;
	int a[7] = {1, 2, 5, 10, 20, 50, 100};
	int b[6] = {5, 10, 20, 50, 100, 200};
	scanf("%d%d%d", &n, &d, &e);
	for (int i = 0; i < 7; i++) {
		a[i] *= d;
	}
	for (int i = 0; i < 6; i++) {
		b[i] *= e;
	}
	int ans = 1e9 + 7;
	for (int i = 0; i <= n; i += b[0]) { // 對一歐元能兌換的金額進行枚舉 
		ans = min(ans, (n - i) % a[0]); // 對一美元能兌換的金額進行取模 
	} 
	printf("%d\n", ans);
	return 0;
} 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章