ABC085 C - Otoshidama

Calendar Clock iconCalendar Clock icon

atcoder

Table of contents

# Problem

https://atcoder.jp/contests/abc085/tasks/abc085_c

Given 2 integers NN and YY, report if it's possible to make YY yen with NN bills or not.
A bill is either a 10000 yen, a 5000 yen or a 1000 yen.
If it's possible, print any combination of them.

# Explanation

Letting the numbers of 10000 yen bill, 5000 yen bill and 1000 yen bill aa, bb and cc, you get 2 equations (1) and (2).
And as aa, bb and cc are the number of bills, you get (3).

{a+b+c=N(1)10000a+5000b+1000c=Y(2)0a,b,cN(3)\begin{cases} a + b + c = N (1) \\ 10000a + 5000b + 1000c = Y (2) \\ 0 \leq a, b, c \leq N (3) \end{cases}

From (1) cc is unique for a combination of aa and bb.

c=Nabc = N - a - b

If you find any combination of aa, bb and cc which satisfies (2) and (3), print it and done.

# Time complexity

O(N2)O(N^2)

Double loop for making combinations of aa and bb.

# Solution

// C++ 14
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <math.h>

#define ll long long
#define Int int
#define loop(x, start, end) for(Int x = start; x < end; x++)
#define loopdown(x, start, end) for(int x = start; x > end; x--)
#define rep(n) for(int x = 0; x < n; x++)
#define span(a,x,y) a.begin()+x,a.begin()+y
#define span_all(a) a.begin(),a.end()
#define len(x) (x.size())
#define last(x) (*(x.end()-1))

using namespace std;
#define MAX_N 2001
#define MAX_Y 20000001

Int N, Y;

void input() {
  cin >> N >> Y;
}

void solve() {
  loop(a,0,N+1) {
    loop(b,0,N+1) {
      Int c = N - a - b;
      if (a + b > N || c < 0) continue;
      if (a * 10000 + b * 5000 + c * 1000 == Y) {
        cout << a << ' ' << b << ' ' << c << endl;
        return;
      }
    }
  }
  cout << "-1 -1 -1" << endl;
}

int main(void) {
  input();
  solve();
  return 0;
}

Remote freelancer. A web and mobile application enginner.
Traveling around the world based on East Asia.
I'm looking forward to your job offers from all over the world!

Offer jobs or contact me!

Comments