题目描述
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
【题意解析】
将炮弹按如图的金字塔方式堆放,从左到右分别为第一堆、第二堆、第三堆和第四堆,以此叠加。现在给一个序号(从第一堆开始算起),判断该序号标记的炮弹在第几堆第几行第几列。
【思路】
两次利用二分的思想,第一次二分确定该炮弹在哪一堆,第二次二分确定在哪一行,以此来确定所在的列。
【代码】
#include<iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 2000017
typedef long long ll;
ll p[N]={0},sum[N]={0};//数组p存储第i堆所包含的炮弹数,数组sum存储前i堆炮弹的总数
ll findd1(ll n)
{
ll s=1,e=N,mid;
while(s<e)
{
mid=(s+e)/2;
if(sum[mid]<n)
s=mid+1;
else if(sum[mid]>n)
e=mid-1;
else
return mid;
}
return s;
}
ll findd2(ll n,ll end)
{
int s=1,e=N,mid;
while(s<e)
{
mid=(s+e)/2;
if(p[mid]>n)
e=mid-1;
else if(p[mid]<n)
s=mid+1;
else
return mid;
}
return s;
}
int main()
{
ll t,n;
memset(p,0,sizeof(p));
memset(sum,0,sizeof(sum));
for(int i=1;i<N;i++)
{
p[i]=p[i-1]+i;
}
for(int i=1;i<N;i++)
{
sum[i]=sum[i-1]+p[i];
}
scanf("%lld",&t);
while(t--)
{
scanf("%lld",&n);
ll wz=findd1(n);
if(sum[wz]<n)
wz+=1;
ll i,j,s;
s=n-sum[wz-1];//s是所在堆的炮弹数
i=findd2(s,wz);
if(p[i]==s)
{
printf("%lld %lld %lld\n",wz,i,i);
}
else
{
if(p[i]<s)
i++;
j=s-p[i-1];
printf("%lld %lld% lld\n",wz,i,j);
}
}
return 0;
}
在网上看到一种找规律的方法,很简洁,但是一般想不到
#include<cstdio>
#include<cmath>
#define ll unsigned long long
int main()
{
int t;ll n;
scanf("%d",&t);
while(t--)
{
scanf("%lld",&n);
ll p=(ll)pow(n*6.0,1.0/3);
p++;
while((p*p*p-p)>=n*6.0)
{
p--;
}
n-=(p*p*p-p)/6;
ll k=(ll)(sqrt(2.0*n));
k++;
while(k*(k+1)/2>=n)
{
k--;
}
ll j =k+1,c=n-k*(k+1)/2;
printf("%lld %lld %lld\n",p,j,c);
}
return 0;
}