http://acm.hdu.edu.cn/showproblem.php?pid=4342
当出现和long long相关的操作时 一定要注意定义的int 变量在相加或相乘时是否会越界
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<map> #include<cmath> #define LL long long using namespace std; const int N=100005; LL asum[N]; LL num[N]; void bigen() { asum[1]=1; num[1]=0; for(LL i=2;i<N;++i)//i也得变成long long 因为相乘时会超过int { num[i]=(i*i-(i-1)*(i-1))-1+num[i-1]; asum[i]=(i*i-(i-1)*(i-1))*(i-1)+1+asum[i-1]; } } int main() { //freopen("data.txt","r",stdin); int T; bigen(); cin>>T; while(T--) { LL n; cin>>n; LL m; for(int i=1;i<N;++i) { if(num[i]>=n) { m=i-1; break; } } LL ans1=m*m+(n-num[m]); LL ans2=asum[m]+(n-num[m])*m; cout<<ans1<<" "<<ans2<<endl; } return 0; }