Codeforces Round #639 (Div. 2) B.Card Constructions

A card pyramid of height 11 is constructed by resting two cards against each other. For h>1h>1, a card pyramid of height hh is constructed by placing a card pyramid of height h1h−1 onto a base. A base consists of hh pyramids of height 11, and h1h−1 cards on top. For example, card pyramids of heights 11, 22, and 33 look as follows:

You start with nn cards and build the tallest pyramid that you can. If there are some cards remaining, you build the tallest pyramid possible with the remaining cards. You repeat this process until it is impossible to build another pyramid. In the end, how many pyramids will you have constructed?

Input

Each test consists of multiple test cases. The first line contains a single integer tt (1t10001≤t≤1000) — the number of test cases. Next tt lines contain descriptions of test cases.

Each test case contains a single integer nn (1n1091≤n≤109) — the number of cards.

It is guaranteed that the sum of nn over all test cases does not exceed 109109.

Output

For each test case output a single integer — the number of pyramids you will have constructed in the end.

Example
Input
Copy
5
3
14
15
24
1
Output
Copy
1
2
1
3
0
预处理出不同层数对应的金字塔所需要的边,然后二分查找第一个小于等于n的数,累加答案更新n继续进行直到n<2。
别看代码,直接从最大的数往下遍历就好(不用每次都二分
#include <bits/stdc++.h>
using namespace std;
int h[100005];
int main()
{
    int i;
    h[1]=2;
    for(i=2;i<=100000;i++)
    {
        h[i]=2+(i-1)*5+(i-1)*(i-2)/2*3;
    }
    int t;
    cin>>t;
    while(t--)
    {
        int n,ans=0;
        cin>>n;
        while(n>=2)
        {
            int pos=lower_bound(h+1,h+100000+1,n)-h;
            if(h[pos]==n)
            {
                ans++;
                break;
            }
            else pos--;
            if(pos<1||pos>100000)break;
            ans++;
            n-=h[pos];
        }
        cout<<ans<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/lipoicyclic/p/12873382.html