您的位置:首页 > 其它

hdu 4342 数学规律

2012-08-09 11:25 351 查看
/*
首先先根据n求k,k是满足不等式  k*k-k<n的最大正整数。
不等式的解是:
(1+sqrt(1+4*n))/2;必需对这个数上取整,然后减一就是k了。
那么 idx=n+k;
然后再根据 idx和k 求 ans;
LL sum=k*idx;
LL tmp=(1+k)*k*(2*k+1)/6-k;
ans=sum-tmp;
idx 和 ans 即为要求结果!!
*/
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
#define LL long long
int t;
LL n,idx,ans;
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%lld",&n);
idx=(LL)ceil((1+sqrt(1+4.0*n))/2)-1+n;
LL k=idx-n;
LL sum=k*idx;
LL tmp=(1+k)*k*(2*k+1)/6-k;
ans=sum-tmp;
cout<<idx<<" "<<ans<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: