A. Optimal Currency Exchange 兑换硬币,剩下的钱最少

A. Optimal Currency Exchange

time limit per test
1.5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard 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 nn 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 dd rubles, and one euro costs ee rubles.

Recall that there exist the following dollar bills: 11, 22, 55, 1010, 2020, 5050, 100100, and the following euro bills — 55, 1010, 2020, 5050, 100100, 200200 (note that, in this problem we do not consider the 500500 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 nn, ee and dd, 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 nn (1n1081≤n≤108) — the initial sum in rubles Andrew has.

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

The third line of the input contains integer ee (30e10030≤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
Copy
100
60
70
output
Copy
40
input
Copy
410
55
70
output
Copy
5
input
Copy
600
60
70
output
Copy
0
Note

In the first example, we can buy just 11 dollar because there is no 11 euro bill.

In the second example, optimal exchange is to buy 55 euro and 11 dollar.

In the third example, optimal exchange is to buy 1010 dollars in one bill.

扫描二维码关注公众号,回复: 7446450 查看本文章

 题意:用卢比换美元和欧元,要使剩下的卢比数量最少

题解:给定美元和欧元的汇率分别是 d 和  e ,d是1的倍数,e是5的倍数,枚举x( x<=n ),输出min(ans,x%d+(n-x)%e)即可

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
    int n,d,e,ans=99999999;
    cin>>n>>d>>e;
    for(int i=0;i<=n;i++)
        ans=min(ans,i%d+(n-i)%e);
    cout<<ans<<endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/-citywall123/p/11650321.html
今日推荐