4 1 3 6 10
2 2 5 7 8 13 13 28
#include<bits/stdc++.h> using namespace std; typedef __int64 INT; INT dfs(INT c){ INT cc=(INT)sqrt(c); if(cc==0){ return 0; } INT ret=(c-cc*cc+1)*cc; return ret+dfs(cc*cc-1); } int main(){ int t; INT n,m; scanf("%d",&t); while(t--){ scanf("%I64d",&n); INT nn=(INT)sqrt(n); m=n+nn; INT mm=(INT)sqrt(m); if(mm>nn) //这条件开始写错了 m++; INT ans=dfs(m); printf("%I64d %I64d\n",m,ans); } return 0; }
思路2:数学推导。http://www.cnblogs.com/kuangbin/archive/2012/08/08/2628794.html
#include<bits/stdc++.h> using namespace std; typedef __int64 INT; INT dfs(INT c){ INT cc=(INT)sqrt(c); if(cc==0){ return 0; } INT ret=(c-cc*cc+1)*cc; return ret+dfs(cc*cc-1); } int main(){ int t; INT n,m; scanf("%d",&t); while(t--){ scanf("%I64d",&n); INT mm=(INT)ceil((1+sqrt(1+4*n))/2)-1; m=mm+n; INT ans=dfs(m); printf("%I64d %I64d\n",m,ans); } return 0; }
HDU 4342——History repeat itself——————【数学规律】
原文:http://www.cnblogs.com/chengsheng/p/4766050.html