## Pigeonhole Tower

Pigeon SSNA want to build a tower with some wood walls. Let’s describe the tower they want to make:

A Tower can consist of different number of level.
If a tower contain L levels then 1st level must contain L holes , 2nd level L-1 , 3rd level L-2 ….. L level contain 1 hole .
Each room contain 3 wood walls.
See the picture below:

3 level 4level
3 Level Tower 4 Level tower
Now pigeon SSNA has n wood walls. What is maximum number of level he can made.

Input

Input starts with an integer T (≤ 100000), denoting the number of test cases.

Each case starts with a line containing an integer n (1 ≤ n ≤ 1012)

Output

For each case of input you have to print the case number and expected answer.

Sample Input

Output for Sample Input

2
15
24
Case 1: 3
Case 2: 4

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<math.h>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
const int inf = 0x3f3f3f3f;
long long n;
int bs(){
long long lo=1,hi=10e6;
long long mid;
while (lo<hi){
mid=(hi+lo)/2;
//如果找到区间就直接返回
if (n<mid*(mid+2)&&n>=(mid-1)*(mid+1)) return mid-1;
if (n>=mid*(mid+2))lo=mid;
else hi=mid;
}
}
int main ()
{
int t;
scanf ("%d",&t);
int cnt=1;
while (t--){
scanf ("%lld",&n);
if (n<3){
printf ("Case %d: 0\n",cnt++);
continue;
}
int ans=bs();
printf ("Case %d: %d\n",cnt++,ans);
}
return 0;
}

#### SPOJ PHT Pigeonhole Tower 预处理+二分 || 找规律

2016-12-25 22:34:41

#### SPOJ - PHT【二分+预处理】

2016-12-26 19:08:35

#### SPOJ-ZQUERY(分块)

2017-09-15 19:41:17

#### SPOJ - PHT

2017-06-24 16:52:52

#### B - Pigeonhole Tower

2017-04-25 20:49:58

#### 鸽巢原理（Pigeonhole Principle）

2015-07-22 21:16:10

#### 经典排序算法 - 鸽巢排序Pigeonhole sort

2014-12-16 11:51:13

#### SPOJ PHT【二分】+SPOJ INUM【最小/大值重复】

2016-12-25 00:08:01

#### zoj 3460 Missile 【二分 + 二分图匹配】 【经典建模】 【二分 + 最大流】

2015-08-02 21:27:45

#### 鸽巢排序Pigeonhole Sort----(排序算法八)

2013-10-24 19:50:57