CodeForces - 1A
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.
The input contains three positive integer numbers in the first line: n, m and a (1 ≤ n, m, a ≤ 10^9).
Write the needed number of flagstones.
6 6 4
4
比较水的一道题,需要注意变量大小要开成long long
#include
typedef long long ll;
int main(void)
{
ll n, m, a;
bool x = 0, y = 0;
scanf("%lld%lld%lld", &n, &m, &a);
if (n % a != 0)
x = 1;
if (m % a != 0)
y = 1;
n /= a;
m /= a;
if (x)
n += 1;
if (y)
m += 1;
printf("%lld\n", n * m);
return 0;
}
CodeForces - 1B
链接:https://blog.csdn.net/weixin_43772166/article/details/100050789
CodeForces - 1C
Nowadays all circuses in Berland have a round arena with diameter 13 meters, but in the past things were different.
In Ancient Berland arenas in circuses were shaped as a regular (equiangular) polygon, the size and the number of angles could vary from one circus to another. In each corner of the arena there was a special pillar, and the rope strung between the pillars marked the arena edges.
Recently the scientists from Berland have discovered the remains of the ancient circus arena. They found only three pillars, the others were destroyed by the time.
You are given the coordinates of these three pillars. Find out what is the smallest area that the arena could have.
The input file consists of three lines, each of them contains a pair of numbers –– coordinates of the pillar. Any coordinate doesn’t exceed 1000 by absolute value, and is given with at most six digits after decimal point.
Output the smallest possible area of the ancient arena. This number should be accurate to at least 6 digits after the decimal point. It’s guaranteed that the number of angles in the optimal polygon is not larger than 100.
0.000000 0.000000
1.000000 1.000000
0.000000 1.000000
1.00000000
大致思路:先求边,再利用海伦公式外接圆半径,进而求出圆心角。求出三个圆心角的gcd,多边形的边数(即三角形个数)为2 * pi / gcd,求出一个三角形的面积,乘以边数即为最小面积。
需要注意:
1.精度问题:求第三个圆心角时,要用2 * pi减去其他两个角来提高精度。
2.超时问题:我一开始用asin来求角度,每次都会超时,后来改成了acos就过了。
还要学习一下小数取模和小数gcd
题解:
https://blog.csdn.net/u012861385/article/details/24273627
https://blog.csdn.net/eric1561759334/article/details/88419921
#include
#include
const double pi = 3.1415926;
double mod(double a, double b)//小数取模
{
return a - (int)(a / b) * b;
}
double gcd(double x, double y)//小数gcd
{
if(y <= 0.0001) return x;
return gcd(y, mod(x, y));
}
int main(void)
{
double x1, y1, x2, y2, x3, y3;
double l1, l2, l3;//三条边
double cos1, cos2, cos3;//三个角的cos值
double a1, a2, a3;//三角形对应的三个圆心角
double gc = 10;//三角形三个角的最大公约数
double p, s;//海伦公式求面积
double r;//外接圆半径
scanf("%lf%lf", &x1, &y1);
scanf("%lf%lf", &x2, &y2);
scanf("%lf%lf", &x3, &y3);
l1 = sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3));
l2 = sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3));
l3 = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
//printf("%lf %lf %lf\n", l1, l2, l3);
p = (l1 + l2 + l3) / 2;
s = sqrt(p * (p - l1) * (p - l2) * (p - l3));
//printf("%lf\n", s);
r = l1 * l2 * l3 / (4 * s);
//printf("%lf\n", r);
cos1 = (l2 * l2 + l3 * l3 - l1 * l1) / (2 * l2 * l3);
cos2 = (l1 * l1 + l3 * l3 - l2 * l2) / (2 * l1 * l3);
//printf("%lf %lf %lf\n", cos1, cos2, cos3);
a1 = 2 * acos(cos1);
a2 = 2 * acos(cos2);
a3 = 2 * pi - a1 - a2;//直接求第三个角,为了提高精度
//printf("%lf %lf %lf\n", a1, a2, a3);
if (gc > gcd(a1, a2))
gc = gcd(a1, a2);
if (gc > gcd(a1, a3))
gc = gcd(a1, a3);
if (gc > gcd(a2, a3))
gc = gcd(a2, a3);
//printf("%lf\n", gc);
double num = 2 * pi / gc;//表示三角形数目
s = num * (r * r) * sin(gc) / 2;
printf("%.6lf\n", s);
return 0;
}