-1

I am solving problem 1A : Theatre Square(https://codeforces.com/problemset/problem/1/A) on Codeforces.

Theatre Square in the capital city of Berland has a rectangular shape with the size n × m meters. On the occasion of the city's anniversary, a decision was taken to pave the Square with square granite flagstones. Each flagstone is of the size a × a.

What is the least number of flagstones needed to pave the Square? It's allowed to cover the surface larger than the Theatre Square, but the Square has to be covered. It's not allowed to break the flagstones. The sides of flagstones should be parallel to the sides of the Square.

Input : The input contains three positive integer numbers in the first line: n,  m and a (1 ≤  n, m, a ≤ 10^9).

Output : Write the needed number of flagstones.

The following code does not work, when I print the answer directly using cout:

#include <bits/stdc++.h>

using namespace std;

int main() {
    long long n, m, a;

    cin >> n >> m >> a;

    cout << (ceil(double(n) / a) * ceil(double(m) / a)) << endl;    
    return 0;
}

But works when I store the answer and then print it :

#include <bits/stdc++.h>

using namespace std;

int main(){
    long long n, m, a, number;

    cin >> n >> m >> a;

    number = (ceil(double(n) / a) * ceil(double(m) / a));    
    cout << number << endl;

    return 0;
}

Please help me understand why it gives wrong answer in the first case for the following input:

n = m = 10^9 ; a = 1
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
tarungaur
  • 21
  • 4
  • For which test case it gave WA? – Md. Faisal Habib Jul 16 '23 at 07:44
  • 5
    You're printing a `double` in one case and a `long long` in the other. You shouldn't be using floating point arithmethic here at all. – fabian Jul 16 '23 at 07:44
  • Test Case 9 : n = m = 10^9 ; a = 1 – tarungaur Jul 16 '23 at 07:45
  • 6
    While we're at it, read the following: [Why should I not include bits/stdc++.h](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is using namespace std considered bad practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – infinitezero Jul 16 '23 at 07:45
  • @fabian thanks for the clarification! infinitezero thanks for the links, I will surely go through them and keep the good practices in mind. – tarungaur Jul 16 '23 at 07:46
  • You could just replace `ceil` with adding `(a - 1)` and using integral division: `uint64_t n; uint64_t m; uint364_t m; std::cin >> n >> m >> a; std::cout << (((m + a - 1) / a) * ((n + a - 1) / a)) << '\n';` – fabian Jul 16 '23 at 07:48
  • 8
    Please don't use such sites to learn programming or programming languages, that's not their purpose. If you're serious about learning C++ then please invest in [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to learn it properly first. – Some programmer dude Jul 16 '23 at 07:49
  • 3
    @tarungaur -- Do not use floating point functions or values to solve integer-based problems. That's the bottom line. If you find yourself using `ceil()`, `pow()` `double`, etc. to solve a problem that is solely integer-based, any answer you come up with is (almost) automatically invalid. – PaulMcKenzie Jul 16 '23 at 08:59

1 Answers1

3

As noted, in the first case you are using std::ceil which returns a double and are printing that type of value. In the second case you are assigning that double value to a variable of type long long which is an integer type, and then printing that long long value.

The two should not be expected to print the same.

Chris
  • 26,361
  • 5
  • 21
  • 42