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.
To find how many bundles of pens we should order, we need to group the pens in bundles of . By dividing by , we can get the quotient and remainder . As a mathematical expression, we can denote this as . This means by grouping the pens in bundles of , we get complete bundles and remaining. These 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.
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 in bundles of . Let's call the quotient and remainder of this division respectively.
If , we can distribute T-shirts without any leftovers by ordering exactly bundles. The answer is .
If , more T-shirts must be ordered after ordering bundles. Since T-shirts cannot be ordered one by one, we need to order one more bundle. Thus, the answer is .
This can be implemented as follows in the following languages. Variable names are S, T
respectively, and the answer is res
.
int q = S / T, r = S % T;
int res = q + (r > 0 ? 1 : 0);
q, r = S // T, S % T
res = q + (1 if r > 0 else 0)
let q = S / T;
let r = S % T;
let res = q + if r > 0 { 1 } else { 0 };
We can write the code without using if
statements too. First let's define the following symbols.
The quotient in the integer division of by is . After dividing by , this is obtained by removing the fractional part that we cannot group in bundles of . However, we need to find in this problem. Even if there is a fractional part remaining, we need to think of it as complete bundle.
Now, let's prove the following. For an integer greater than or equal to , and an integer greater than or equal to , .
If is a multiple of , , and . Since is an integer, and is ignored as it is smaller than , .
If is not a multiple of , is not an integer, so . Furthermore, since there is at least remainder from , we get the same quotient when dividing by . That is, . .
We can make our code much simpler using this method. Below are example solutions written in each language.
#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;
}
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()
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()
}