Sehr Sus is an infinite hexagonal grid as pictured below, controlled by MennaFadali, ZerooCool and Hosssam.
They love equilateral triangles and want to create nn equilateral triangles on the grid by adding some straight lines. The triangles must all be empty from the inside (in other words, no straight line or hexagon edge should pass through any of the triangles).
You are allowed to add straight lines parallel to the edges of the hexagons. Given nn, what is the minimum number of lines you need to add to create at least nn equilateral triangles as described?
Adding two red lines results in two new yellow equilateral triangles.
Input
The first line contains a single integer tt (1≤t≤1051≤t≤105) — the number of test cases. Then tt test cases follow.
Each test case contains a single integer nn (1≤n≤1091≤n≤109) — the required number of equilateral triangles.
Output
For each test case, print the minimum number of lines needed to have nn or more equilateral triangles.
Example
input
Copy
4 1 2 3 4567
output
Copy
2 2 3 83
Note
In the first and second test cases only 2 lines are needed. After adding the first line, no equilateral triangles will be created no matter where it is added. But after adding the second line, two more triangles will be created at once.
In the third test case, the minimum needed is 3 lines as shown below.
思路:这个题不是说是正六边形,要找到正三角形,我们可以从角度入手,我们是不是只要凑出60度角即可,因为要平行边所以相当于只有3种边,1.平行x轴。2.与x轴夹角60度。3.与x轴夹角120度。我们的边只有这3种选择,我们是不是只要将两条边的夹角凑成60度就会有一个等边三角形,因为另一条边是六边形的边。有一个60度形成,就会有两个等边三角形,因为它的对角也是60度。然后就是边与边的关系(组合),假设三个边为x,y,z 。三个边的组合有,x与y,y与z,x与z,而且以上述的三种边组合,只要它们相交就一定会有60度,所以只要知道x这种边的个数,乘以y这种边的个数,同理推其余两种。有60度就是有两三角形,所以就会有 2*(x*y + y*z + x*z),然后就是x,y,z三种边越平均越好,每次都是不同种类就能有更多交点,所以不同种的边一个一个排下来就好,均匀分配,n个边不能被整除时就让它们添上一点,不要浪费,不害怕角多。那么我们直接二分边数找到最大中的最小即可。
完整代码:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod=1e9+7;
int check(int mid)
{
int x=mid/3,y=mid/3,z=mid/3;
if(x+y+z<mid)x++;
if(x+y+z<mid)y++;
if(x+y+z<mid)z++;
return 2*(x*y+y*z+x*z);
}
void solve()
{
int n;
cin>>n;
int l=0,r=1e5;
while(l<r)
{
int mid=l+r>>1;
if(check(mid)>=n)r=mid;
else l=mid+1;
}
cout<<r<<endl;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--)
{
solve();
}
return 0;
}