Problem Description
Calculate S(n).
S(n)=13+23 +33 +......+n3 .
S(n)=13+23 +33 +......+n3 .
Input
Each line will contain one integer N(1 < n < 1000000000). Process to end of file.
Output
For each case, output the last four dights of S(N) in one line.
Sample Input
1 2
Sample Output
0001 0009
解题思路:
这题主要利用公式计算:13 +23 +33 +……+n3 =[n(n+1)/2]2
刚开始是是直接算,发现被TE了,后来网上看到了公式,原来是利用公式计算,当然输出时用printf("%04I64d/n",sum);如果前面没有则补零。
AC代码:
#include <stdio.h>
int main()
{
__int64 n,sum;
while (scanf("%I64d",&n)!=EOF)
{
sum=0;
n=n%10000;
sum=(n*(n+1)/2)*(n*(n+1)/2)%10000;
printf("%04I64d/n",sum);
}
return 0;
}

被折叠的 条评论
为什么被折叠?



