首页 > ACM题库 > HDU-杭电 > hdu 2446 Shell Pyramid-分治-[解题报告]C++
2014
01-26

hdu 2446 Shell Pyramid-分治-[解题报告]C++

Shell Pyramid

问题描述 :

In the 17th century, with thunderous noise, dense smoke and blazing fire, battles on the sea were just the same as those in the modern times. But at that time, the cannon ,were extremely simple. It was just like an iron cylinder, with its rearward end sealed and forward end open. There was a small hole at the rearward end of it, which was used to install the fuse. The cannons on the warships were put on small vehicles which had four wheels and the shells were iron spheres with gunpowder in them.At that time, it was said that there was an intelligent captain, who was also a mathematician amateur. He liked to connect everything him met to mathematics. Before every battle, he often ordered the soldiers to put the shells on the deck and make those shells to form shell pyramids.

Now let’s suppose that a shell pyramid has four layers, and there will be a sequence of ordinal numbers in every layer. They are as the following figure:

In the figure, they are the first layer, the second layer, the third layer and the fourth layer respectively from the left to the right.

In the first layer, there is just 1 shell, and its ordinal number is 1. In the second layer, there are 3 shells, and their ordinal numbers are 1, 2, and 3. In the third layer, there are 6 shells, and their ordinal numbers are 1, 2, 3, 4, 5, and 6. In the fourth layer, there are 10 shells, and their ordinal numbers are shown in the figure above.

There are also serial numbers for the whole shell pyramid. For example, the serial number for the third shell in the second layer is 4, the serial number for the fifth shell in the third layer is 9, and the serial number for the ninth shell in the fourth layer is 19.

There is also a interrelated problem: If given one serial number s, then we can work out the s th shell is in what layer, what row and what column. Assume that the layer number is i, the row number is j and the column number is k, therefore, if s=19, then i=4, j=4 and k=3.

Now let us continue to tell about the story about the captain.
A battle was going to begin. The captain allotted the same amount of shells to every cannon. The shells were piled on the deck which formed the same shell pyramids by the cannon. While the enemy warships were near, the captain ordered to fire simultaneously. Thunderous sound then was heard. The captain listened carefully, then he knew that how many shells were used and how many were left.

At the end of the battle, the captain won. During the break, he asked his subordinate a question: For a shell pyramid, if given the serial number s, how do you calculate the layer number i, the row number j and column number k?

输入:

First input a number n,repersent n cases.For each case there a shell pyramid which is big enough, a integer is given, and this integer is the serial number s(s<2^63). There are several test cases. Input is terminated by the end of file.

输出:

First input a number n,repersent n cases.For each case there a shell pyramid which is big enough, a integer is given, and this integer is the serial number s(s<2^63). There are several test cases. Input is terminated by the end of file.

样例输入:

2
19
75822050528572544

样例输出:

4 4 3
769099 111570 11179

**====================================================**
题意:这道题说的是叠金字塔啦,最顶层是1,第二层是3,之后
每一层的数量都是上面一层的数量加当前层数的值啦。所以呢,
一层金字塔有1个球,两层金字塔有1+3 = 4个球,三层金字塔就
有1+3+6 = 10个的球。现在给你一个编号s,问你它在金字塔中的
第几层,第几行,第几列。例如19,它就在第四层,第四行,第
三列。
分析:输入的编号太大,2^63,打表求和,之后二分查找,判段
在第几层。再进行一次二分查找,判定在第几行和列。
**====================================================**

08 哈尔滨区域赛的题目 ,   数学题。

题意就是一系列的数按照金字塔这样排列:
如  第一层有1个数        1
     第二层有3个数        2
                                  3     4
     第三层有6个数                  5
                                      6     7
                                  8     9    10
  。。。。。
  给定一个数  让你求它在第几层第几行第几列  。
第n层共有n*(n+1)/2 ;
前n层共有(1*2+2*3+3*4+…+n*(n+1))/2 ->  n*(n+1)*(n+2) / 6;
#include<stdio.h>
#include<math.h>
typedef long long LL ;
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        LL layer,row,col ;
        LL n;
        scanf("%I64d",&n) ;
        layer = (LL)(pow(double(n) * 6.0 , 1.0/3.0)) - 1;
        while(layer*(layer+1)/2*(layer+2)/3 < n ){
            layer ++ ;
        }
         n -= (layer-1)*(layer)/2*(layer+1)/3 ;
        row = (LL )(sqrt(n * 2.0)) ;
        while(row * (row+1)/2 < n)
            row ++ ;
        n -=row*(row-1)/2 ;
        col = n;
        printf("%I64d %I64d %I64d\n",layer,row,col) ; 
    }
    return 0;
}

  1. 其实国内大部分公司对算法都不够重视。特别是中小型公司老板根本都不懂技术,也不懂什么是算法,从而也不要求程序员懂什么算法,做程序从来不考虑性能问题,只要页面能显示出来就是好程序,这是国内的现状,很无奈。