hdu 2446 Shell Pyramid(GOD = =)

来源:互联网 发布:js array 删除指定元素 编辑:程序博客网 时间:2024/06/10 00:17

开始想打表二分来着,后来想试试能不能推出来公式,结果还真推出来了 = =,很简单的公式。

第n-1堆的最大的序号是,(n*n*n - n)/6。

然后对n*6开立方根可以得到n(细节细节)。。。

再找到这一堆的大小(细节细节)。。

再根据1+2+3。。。求和公式找到第几行(细节细节)。。

再根据前N行的和找到在第N+1行的第几列(细节细节)。。。

= = 。。注意别超unsigned __int64。。中间可以用double计算防超。不过显然数据很小。。


#include <map>#include <set>#include <queue>#include <stack>#include <math.h>#include <time.h>#include <stdio.h>#include <stdlib.h>#include <iostream>#include <limits.h>#include <string.h>#include <string>#include <algorithm>#define MID(x,y) ( ( x + y ) >> 1 )#define L(x) ( x << 1 )#define R(x) ( x << 1 | 1 )#define BUG puts("here!!!")#define STOP system("pause")using namespace std;typedef unsigned __int64 ll;int main(){int ncases;ll n;scanf("%d", &ncases);while( ncases-- ){scanf("%I64u", &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 ans1 = k + 1;ll ans2 = n - k*(k+1)/2;printf("%I64u %I64u %I64u\n", p, ans1, ans2);}return 0;}


原创粉丝点击