您的位置:首页 > 编程语言 > Go语言

codeforces 560C Gerald's Hexagon (数学+思维)

2017-05-11 20:01 537 查看
题目原文:http://codeforces.com/contest/560/problem/C

Gerald got a very curious hexagon for his birthday. The boy found out that all the angles of the hexagon are equal to


. Then he measured the length of its sides, and found that each of them is
equal to an integer number of centimeters. There the properties of the hexagon ended and Gerald decided to draw on it.

He painted a few lines, parallel to the sides of the hexagon. The lines split the hexagon into regular triangles with sides of 1 centimeter. Now Gerald wonders how many triangles he has got. But there were so many of them that Gerald lost the track of his
counting. Help the boy count the triangles.

Input
The first and the single line of the input contains 6 space-separated integers
a1, a2, a3, a4, a5
and a6 (1 ≤ ai ≤ 1000) — the lengths
of the sides of the hexagons in centimeters in the clockwise order. It is guaranteed that the hexagon with the indicated properties and the exactly such sides exists.

Output
Print a single integer — the number of triangles with the sides of one 1 centimeter, into which the hexagon is split.

题目大意:有一个6个角都是120度的六边形,给你六个边的边长,问可以用多少个边长为1的等边三角形将这个六边形铺满。

解题思路:

尝试过推公式,不过发现比较麻烦。

后来突然想到了面积法,三角形密铺必然最后等于六边形的面积,所以我们只需要求出这个六边形的面积。

现在问题变成了怎么快速求出这个六边形的面积?

最开始尝试用三角划分,不过很容易出现小数,精度没有办法保证,后来突然想到这种六边形是可以从一个大的等边三角形切下三条边得到。大三角形的边长很好求得,三个小三角形的边长也很好求,所以可以得到最后的公式。

ans = (a+b+c)^2 - (a^2 + c^2 + e^2)

AC代码:

/*
@Author: wchhlbt
@Date: 2017/5/11
*/
#include <bits/stdc++.h>

#define Fori(x) for(int i=0;i<x;i++)
#define Forj(x) for(int j=0;j<x;j++)
#define maxn 1111111
#define inf 0x3f3f3f3f
#define ONES(x) __builtin_popcount(x)
using namespace std;

typedef long long ll ;
const double eps =1e-8;
const int mod = 1000000007;
typedef pair<int, int> P;
const double PI = acos(-1.0);
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};

ll n,m,ans;
ll a,b,c,d;
int main()
{
//freopen("input.txt","r",stdin);
int a,b,c,d,e,f;
cin>>a>>b>>c>>d>>e>>f;
int l = (a+b+c);
cout << (l*l) - (a*a + c*c + e*e) << endl;
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: