ArenaRating

solved.ac Grand Arena #3

In this problem, we need to calculate the number of bundles of T-shirts of each size, and the number of bundles of pens that we need to order. First, let's look at how we can find the number of bundles of pens as this is simpler.

Pens

To find how many bundles of pens we should order, we need to group the NN pens in bundles of PP. By dividing NN by PP, we can get the quotient qq and remainder rr. As a mathematical expression, we can denote this as NΓ·P=qβ‹―rN \div P = q \cdots r. This means by grouping the pens in bundles of PP, we get qq complete bundles and rr remaining. These q,rq, r values are the number of bundles of pens that we need to order, and the number of pens we need to order one by one, respectively.

T-Shirts

Solution 1

Now let's find the number of bundles of T-shirts of size S that we need to buy.

We use division again to group T-shirts of size SS in bundles of TT. Let's call the quotient and remainder of this division q,rq, r respectively. (SΓ·T=qβ‹―r)(S \div T = q \cdots r)

This can be implemented as follows in the following languages. Variable names are S, T respectively, and the answer is res .

C++

int q = S / T, r = S % T; int res = q + (r > 0 ? 1 : 0);

Python

q, r = S // T, S % T res = q + (1 if r > 0 else 0)

Rust

let q = S / T; let r = S % T; let res = q + if r > 0 { 1 } else { 0 };

Solution 2

We can write the code without using if statements too. First let's define the following symbols.

The quotient in the integer division of SS by TT is ⌊STβŒ‹\left\lfloor \frac{S}{T} \right \rfloor. After dividing SS by TT, this is obtained by removing the fractional part that we cannot group in bundles of TT. However, we need to find ⌈STβŒ‰\left \lceil \frac{S}{T} \right \rceil in this problem. Even if there is a fractional part remaining, we need to think of it as 11 complete bundle.

Now, let's prove the following. For an integer SS greater than or equal to 00, and an integer TT greater than or equal to 11, ⌈STβŒ‰=⌊S+Tβˆ’1TβŒ‹\left\lceil \frac{S}{T} \right\rceil = \left\lfloor \frac{S+T-1}{T} \right\rfloor.

We can make our code much simpler using this method. Below are example solutions written in each language.

Code (C++)

#include <iostream> #include <vector> using namespace std; int main() { int N; cin >> N; vector<int> sizes(6); for (int &size : sizes) cin >> size; int T, P; cin >> T >> P; int sumv = 0; for (int size : sizes) sumv += (size + T - 1) / T; cout << sumv << endl; cout << N / P << " " << N % P << endl; }

Code (Python)

def main(): N = int(input()) sizes = list(map(int, input().split())) T, P = map(int, input().split()) print(sum((size + T - 1) // T for size in sizes)) print(N // P, N % P) if __name__ == '__main__': main()

Code (Rust)

use std::io::*; fn main() { let n = read_vec()[0]; let sizes = read_vec(); let (t, p) = { let v = read_vec(); (v[0], v[1]) }; println!("{}", sizes.iter().map(|x| (x + t - 1) / t).sum::<u32>()); println!("{} {}", n / p, n % p); } fn read_vec() -> Vec<u32> { let mut s = String::new(); stdin().read_line(&mut s).unwrap(); s.split_whitespace().map(|x| x.parse().unwrap()).collect() }