A. Theatre Square
time limit per test
2 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output
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 ≤ 109).
Output
Write the needed number of flagstones.
Sample test(s)
input
6 6 4
output
4
这道题的大意是有一个n*m面积大小的广场,现在需要用边长为a大小的方砖去铺,方砖可以重叠,输出多少块砖。
我开始是这样写的:
#include <stdio.h>
int main ()
{
long n,m,a;
scanf("%ld%ld%ld",&n,&m,&a);
long nn = (n%a==0 ? n/a:n/a+1);
long mm = (m%a==0 ? m/a:m/a+1);
printf("%ld\n",nn*mm);
}
#include <stdio.h>
int main ()
{
long long n,m,a;
scanf("%I64d%I64d%I64d",&n,&m,&a);
long long nn = (n%a==0 ? n/a:n/a+1);
long long mm = (m%a==0 ? m/a:m/a+1);
printf("%I64d\n",nn*mm);
}
这里还有必要说的是,codeforce支持的是%I64d这种写法,所以应该改变一下%lld的习惯。
当然这里还有必要说一下关于ceil函数(math.h)这个函数可以返回大于某个float数的最小整数,只不过仍是以float形式给出,所以可以看到网上还有人用这样的代码:
#include <cstdio>
#include <cmath>
int main()
{
int m,n,a;
scanf("%d%d%d",&m,&n,&a);
printf("%.lf\n",ceil(m*1.0/a)*ceil(n*1.0/a));
return 0;
}
其实还有一种更简单的办法:(m+a-1)/a这样同样能够得到大于某一个数的最小整数。