HDU 2446 Shell Pyramid(二分查找 )

点击打开链接 原文链接


题目大意:

在图中,它们分别是从左到右的第一层,第二层,第三层和第四层。

在第一层中,只有一个壳,其序号为1.在第二层中,有三个壳,它们的序号是1,2和3.在第三层中,有六个壳,它们的序数分别是1,2,3,4,5和6.第四层中有10个壳,其序号如上图所示。

还有整个金字塔的序列号。例如,第二层中的第三个外壳的序列号是4,第三层中的第五个外壳的序列号是9,第四层中的第九个外壳的序列号是19. 

还有一个相互关联的问题:如果给出一个序列号s,那么我们可以计算出第s个shell是在哪一层,什么行和哪一列。假设层号为i,行号为j,列号为k,因此,如果s = 19,则i = 4,j = 4且k = 3。

现在让我们继续讲述关于队长的故事。
一场战斗即将开始。船长给每个大炮分配相同数量的炮弹。炮弹堆积在甲板上,甲板上的炮塔形成了相同的金字塔。当敌舰靠近时,舰长命令同时开火。当时听到雷鸣般的声音。船长仔细听了,然后他知道有多少炮弹被使用,剩下多少炮弹。

在战斗结束时,队长赢了。休息期间,他问他的下属一个问题:对于贝壳金字塔,如果给出序列号s,你如何计算图层编号i,行号j和列号k?


看的别人的,佩服

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <cmath>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <time.h>;
#define cler(arr, val)    memset(arr, val, sizeof(arr))
#define FOR(i,a,b)  for(int i=a;i<=b;i++)
#define IN   freopen ("in.txt" , "r" , stdin);
#define OUT  freopen ("out.txt" , "w" , stdout);
typedef long long  LL;
const int MAXN = 10323;
const int MAXM = 201;
const int INF = 0x3f3f3f3f;
const int mod = 1000000007;
LL getnum1(LL num)
{
    LL x=num;
    LL y=num+1;
    LL z=num+2;
    if(x%2==0) x/=2;
    else if(y%2==0) y/=2;
    else if(z%2==0) z/=2;


    if(x%3==0) x/=3;
    else if(y%3==0) y/=3;
    else if(z%3==0) z/=3;
    return x*y*z;
}
LL getnum2(LL num)
{
    return num*(num+1)/2;
}
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
    #endif
    int t;
    LL n;
    scanf("%d",&t);
    while(t--)
    {
        cin>>n;
        LL l=0,r=3810776;//r表示极限了。。
        while(l<=r)//求第几层
        {
            LL mid=(l+r)>>1;//getnum1(3810776)
            if(getnum1(mid)>=n)
                r=mid-1;
            else l=mid+1;
        }
        LL ans1=l;
        n-=getnum1(l-1);
        l=0,r=3810776;
        while(l<=r)//求第几行
        {
            LL mid=(l+r)>>1;
            if(getnum2(mid)>=n)
                r=mid-1;
            else l=mid+1;
        }
        n-=getnum2(l-1);
        LL ans2=l;
        printf("%I64d %I64d %I64d\n",ans1,ans2,n);
    }
}

公式 1 : x*(x+1)*(x+2)/ 6 

公式 2 :x*(x+1)/ 2

公式1为公式2 的前n项和


发布了37 篇原创文章 · 获赞 12 · 访问量 6548

猜你喜欢

转载自blog.csdn.net/weixin_38960774/article/details/79390368