Shell Pyramid
时间限制: 1 Sec 内存限制: 128 MB
提交: 291 解决: 95
[提交] [状态] [命题人:admin]
题目描述
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=9, 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?
输入
For 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.
输出
For each case, output the corresponding layer number i, row number j and column number k.
样例输入
复制样例数据
2
19
75822050528572544
样例输出
4 4 3
769099 111570 11179
题目大意:
已知第一层有1个球,第二层有3个球,第三层有6个球,并将第一层的球编号为1,则依次后推,第二层从上到下,从左到右分别是2,3,4,依次后推。现在给你球的编号n,问它是在哪一层的哪一行哪一列上。
解题思路:
首先我们假设每一层球的个数为
n
u
m
[
i
]
num[i]
num[i]
则:
n
u
m
[
i
]
=
i
∗
(
i
+
1
)
/
2
num[i]=i*(i+1)/2
num[i]=i∗(i+1)/2
并假设前
n
n
n层球的个数总和为
s
u
m
[
n
]
sum[n]
sum[n]
则:
s
u
m
[
n
]
=
n
u
m
[
1
]
+
n
u
m
[
2
]
+
⋯
+
n
u
m
[
n
]
sum[n]=num[1]+num[2]+\dots +num[n]
sum[n]=num[1]+num[2]+⋯+num[n]
⇒
s
u
m
[
n
]
=
1
×
2
2
+
2
×
3
2
+
⋯
+
n
×
(
n
+
1
)
2
\Rightarrow sum[n]=\frac{1\times 2}{2}+\frac{2\times 3}{2}+\dots+\frac{n\times(n+1)}{2}
⇒sum[n]=21×2+22×3+⋯+2n×(n+1)
⇒
s
u
m
[
n
]
×
2
=
1
×
2
+
2
×
3
+
⋯
+
n
×
(
n
+
1
)
\Rightarrow sum[n]\times2=1\times2+2\times3+\dots+n\times(n+1)
⇒sum[n]×2=1×2+2×3+⋯+n×(n+1)
⇒
s
u
m
[
n
]
×
2
=
(
1
+
1
2
)
+
(
2
+
2
2
)
+
⋯
+
(
n
+
n
2
)
\Rightarrow sum[n]\times2=(1+1^2)+(2+2^2)+\dots+(n+n^2)
⇒sum[n]×2=(1+12)+(2+22)+⋯+(n+n2)
⇒
s
u
m
[
n
]
×
2
=
1
2
+
2
2
+
⋯
+
n
2
+
1
+
2
+
⋯
+
n
\Rightarrow sum[n]\times2=1^2+2^2+\dots+n^2+1+2+\dots+n
⇒sum[n]×2=12+22+⋯+n2+1+2+⋯+n
⇒
s
u
m
[
n
]
×
2
=
n
×
(
n
+
1
)
(
2
n
+
1
)
6
+
n
×
(
n
+
1
)
2
\Rightarrow sum[n]\times2=\frac{n\times(n+1)(2n+1)}{6}+\frac{n\times(n+1)}{2}
⇒sum[n]×2=6n×(n+1)(2n+1)+2n×(n+1)
⇒
s
u
m
[
n
]
=
n
×
(
n
+
1
)
(
n
+
2
)
6
\Rightarrow sum[n]=\frac{n\times(n+1)(n+2)}{6}
⇒sum[n]=6n×(n+1)(n+2)
所以由前缀和公式我们可以二分找出
n
n
n在哪一层
而每层又是一个首项为
1
1
1公差为
1
1
1的等差数列,因此在二分找出
n
n
n在哪一行即可。
代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
//freopen("out.txt", "w", stdout);
//ios::sync_with_stdio(0),cin.tie(0);
int T;
scanf("%d",&T);
while(T--) {
ll n;
scanf("%lld",&n);
ll l=1,r=10000000;
ll ans=0;
while(r-l>1) {
ll mid=(l+r)>>1;
ll num=mid*(mid+1)/2*(mid+2)/3;
ll num1=(mid+1)*(mid+2)/2*(mid+3)/3;
if(n>num&&n<=num1) {
ans=mid+1;
break;
}
else if(n>num1) {
l=mid;
}
else if(n<=num) {
r=mid;
}
}
if(n==1) ans=1;
if(n>1&&n<=4) ans=2;
ll nape=(ans-1)*ans/2*(ans+1 )/3;
nape=n-nape;
l=1,r=ans;
//cout<<ans<<" "<<nape<<endl;
while(l<r) {
ll mid=(r+l)>>1;
ll num=(mid+1)*mid/2;
if(num<nape) l=mid+1;
else r=mid;
}
//cout<<r<<endl;
ll hang=r;
ll lie;
nape=nape-(hang-1)*hang/2;
lie=nape;
printf("%lld %lld %lld\n",ans,hang,lie);
}
return 0;
}