HDU 2446 shell pyramid (Binary Search mathematics)

Source: Internet
Author: User

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2446


Problem descriptionin 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 second 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?
Inputfirst 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.
Outputfor each case, output the corresponding layer number I, row number J and column number K. sample input
21975822050528572544
Sample output
4 4 3769099 111570 11179
Source2008 Asia Regional Harbin



Question:

Stacked pyramid. The number of top layers is 1, and the number of second layers is 3. Then, the number of each layer is the number of above layers plus the value of the current layer. One layer of pyramid has one ball, two layers of pyramid have 1 + 3 = four balls, and three layers of pyramid have 1 + 3 + 6 = 10 balls.

Now let's give a number "S" and ask the number of layers, rows, and columns in the pyramid.

For example, 19 is in the fourth, fourth, and third columns.

PS:

First, the number of each pyramid and the number of the nth pyramid (that is, the total number of the first n pyramid) are displayed );

Then, the system first finds the pyramid where the given number is located, and then finds out which row and column of the given number are in the current pyramid!

The Code is as follows:

# Include <cstdio> # include <cmath> # include <cstring> # include <algorithm> using namespace STD; typedef _ int64 ll; # define n 2000017ll P [N] = {0}, Sn [N] = {0}; // number of each position of A, B: snll findd1 (LL N) {ll S = 1, E = N, mid; while (S <E) {mid = (S + E)/2; If (Sn [Mid] <n) S = Mid + 1; else if (Sn [Mid]> N) E = mid-1; else return mid;} return s;} ll findd2 (ll n, ll endd) {ll S = 1, E = endd, mid; while (S <E) {mid = (S + E)/2; If (P [Mid] <n) S = Mid + 1; else if (P [Mid]> N) E = mid-1; else return mid;} return s;} int main () {ll T; ll N; memset (p, 0, sizeof (p); memset (Sn, 0, sizeof (SN); For (INT I = 1; I <N; I ++) // each heap {P [I] = P [I-1] + I;} For (INT I = 1; I <n; I ++) // Sn {sn [I] = P [I] + Sn [I-1];}/* For (INT I = N-10; I <n; I ++) {printf ("% i64d \ n", Sn [I]);} * // 2 ^ 63 = 9223372036854775808 scanf ("% i64d", & T ); while (t --) {scanf ("% i64d", & N); LL Weizhi = findd1 (n); // printf ("POS: % i64d \ n ", weizhi); If (Sn [Weizhi] <n) Weizhi + = 1; ll TT = N-Sn [weizhi-1]; // Number of the current heap ll r = findd2 (TT, weizhi); // printf ("R: % i64d \ n", R); If (P [R] = TT) {printf ("% i64d % i64d % i64d \ n", Weizhi, R, R) ;}else {If (P [R] <TT) r ++; ll c = tt-P [r-1]; printf ("% i64d % i64d % i64d \ n", Weizhi, R, c );} /* else {ll c = tt-P [r-1]; printf ("% i64d % i64d % i64d \ n", Weizhi, R, c );} */} return 0;}/* 21415 */


HDU 2446 shell pyramid (Binary Search mathematics)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.