Card Constructions codeforces 1345 B

B. Card Constructions
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
A card pyramid of height 1 is constructed by resting two cards against each other. For h>1, a card pyramid of height h is constructed by placing a card pyramid of height h−1 onto a base. A base consists of h pyramids of height 1, and h−1 cards on top. For example, card pyramids of heights 1, 2, and 3 look as follows:

You start with n 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 t (1≤t≤1000) — the number of test cases. Next t lines contain descriptions of test cases.

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

It is guaranteed that the sum of n over all test cases does not exceed 109.

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

Example
inputCopy
5
3
14
15
24
1
outputCopy
1
2
1
3
0
Note
In the first test, you construct a pyramid of height 1 with 2 cards. There is 1 card remaining, which is not enough to build a pyramid.

In the second test, you build two pyramids, each of height 2, with no cards remaining.

In the third test, you build one pyramid of height 3, with no cards remaining.

In the fourth test, you build one pyramid of height 3 with 9 cards remaining. Then you build a pyramid of height 2 with 2 cards remaining. Then you build a final pyramid of height 1 with no cards remaining.

In the fifth test, one card is not enough to build any pyramids.
解法一 二分层数 层数最多不过30000.

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define ll long long
using namespace std;
ll p(ll n)
{
    return (n*(3*n+1))/2;
}
ll binary(ll l,ll r,ll n)
{
    ll mid;
    ll ans;
    while(l<=r)
    {
        mid=(l+r)>>1;
        ll ti=p(mid);
        if(ti<=n)
        {
            ans=ti;
            l=mid+1;
        }
        else r=mid-1;
    }
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    ll n;
    while(t--)
    {
       scanf("%lld",&n);
       if(n<7) printf("%lld\n",n/2);
       else
       {
           ll cnt=0;
           while(1)
           {
               if(n/7+1<30000)
           {
               ll u=binary(2,n/7+1,n);
               cnt=cnt+n/u;
               n=n%u;
               if(n<7)
               {
                   cnt=cnt+n/2;
                   break;
               }
           }
           else
           {
               ll u=binary(2,30000,n);
               cnt=cnt+n/u;
               n=n%u;
               if(n<7)
               {
                   cnt=cnt+n/2;
                   break;
               }
           }
           }
           printf("%lld\n",cnt);
       }
    }
    return 0;
}

解法二 解方程 一定要用long long 不然和double 转换啥啥的有可能会溢出,还好昨天交不上去,不然又wrong .

#include <iostream>
#include<cstdio>
#include<cmath>
#define ll long long
using namespace std;
double p(double n)
{
    double it=(-1+sqrt(1+24*n))/6;
    return floor(it);
}
ll pp(ll d)
{
    return ((d*3+1)*d)/2;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    ll n;
    while(t--)
    {
        cin>>n;
        ll d=p(n);
        ll cnt=0;
        while(d!=0)
        {
            ll u=pp(d);
            cnt+=n/u;
            n=n%u;
            d=p(n);
        }
        cout<<cnt<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值