Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Donβt think about area first. Since tiles canβt be broken and must stay axis-parallel, try solving the problem in one dimension: how many segments of length are needed to cover a length ?
To cover a side of length with tiles of side , you need the smallest integer such that . Thatβs exactly
Now do the same for the other side: along width , you need tiles. The two directions are independent, which is the whole trick.
So the total number of square flagstones is the product of the counts along both dimensions: Area-based thinking is a trap here, because leftover strips still force whole extra tiles.
Implement the ceiling without floating point: using integer division. Then compute Use long long, because the answer can be as large as about , and int will absolutely eat shit.
This problem is almost insultingly simple once you stop overthinking it.
You have a rectangle of size , and square tiles of size .
That means coverage in the two dimensions is completely independent.
Suppose you only want to cover a segment of length using pieces of length . How many do you need?
You need the smallest integer such that
That is exactly
Same logic for the other side:
Since each tile occupies one βrow slotβ and one βcolumn slotβ, the total number is
Thatβs it. No DP, no geometry wizardry, no sacrifice to the gods of Codeforces.
Do not use floating point here. Thereβs zero reason to invite precision weirdness to a kindergarten math problem.
For positive integers,
using integer division.
So:
Then
The constraints go up to . The answer can be as large as roughly
so int is not enough. Use long long.
The algorithm is constant time:
with
extra memory.
Short, clean, and brutally effective.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
ll n, m, a;
cin >> n >> m >> a;
ll x = (n + a - 1) / a;
ll y = (m + a - 1) / a;
cout << x * y << '\n';
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
ll n, m, a;
cin >> n >> m >> a;
ll x = (n + a - 1) / a;
ll y = (m + a - 1) / a;
cout << x * y << '\n';
return 0;
}