Calculate S(n)
Problem Description
Calculate S(n).
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
主要是计算前n项和的公式。
前n项和的立方公式为 : s(n)=(n*(n+1)/2)^2;
前n项和的平方公式为:s(n)=n*(n+1)(2*n+1)/6;
1 #include<cstdio> 2 #include<iostream> 3 using namespace std; 4 int main() 5 { 6 long long n,sum,t,i; 7 sum=0; 8 while(cin>>n) 9 { 10 long long sum=(((n%10000)*(n+1)%10000)/2)*(((n%10000)*(n+1)%10000)/2)%10000; 11 printf("%04I64d\n",sum); 12 } 13 }
时间: 2024-10-10 15:51:17