Codeforces: A. Theatre Square


The problem is from codeforces: http://www.codeforces.com/problemset/problem/1/A

1A Codeforces: A. Theatre Square algorithms beginner codeforces math programming languages python tricks

Given the size of the area and the size of the flag stone, you are asked to compute the minimal number of flagstones required to cover the square. The problem can be solved by treating each dimension separately.

First, find out the longer edge, we can swap n and m to make sure n is a bigger one. Second, find out the number of flagstones needed to pave in this dimension, ceil function is helpful in this case. Lastly, find out the number of flagstones needed in other dimension, after excluding the first row. The addition of both parts will be the answer.

#!/usr/bin/env python
from math import *

s = raw_input()
ss = s.split(" ")
n = int(ss[0])
m = int(ss[1])
a = int(ss[2])

if (n < m):
    n, m = m, n # swap

sum = 1
top = 0
if (n > a):
    top = int(ceil(n * 1.0 / a))
    sum = top

if (m > a):
    sum += top * (int(ceil((m * 1.0 - a) / a)))

print sum

The above solution works, but not concise enough. The following works like a charm.

#!/usr/bin/env python
from math import *

n, m, a = map(int, raw_input().split())
print int(ceil(n *1.0 / a) * ceil(m * 1.0 / a))

The answer is just ceil(m / a) * ceil(n / a). And we can just use n, m, a = map(int, raw_input().split()) to deal with the inputs.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
332 words
Last Post: Fibonacci Number via Inline Assembly in Delphi
Next Post: Codeforces: B. Simple XML

The Permanent URL is: Codeforces: A. Theatre Square

Leave a Reply