Calculate S(n)
Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10756 Accepted Submission(s): 3878
S(n)=1 3+2 3 +3 3 +......+n 3 .
1 2
0001 0009
这是在hdu看得一道有意思的题,被别人列在水题之列,然而自己并不会...
无奈查了下公式,原来...1^3+2^3+...+n^3=(n(n+1)/2)^2...
GG,然后套公式秒过了。。。
看来这种题得记下公式了。。具体证明
1^2+2^2+3^2+……+n^2=n(n+1)(2n+1)/6
利用立方差公式
n^3-(n-1)^3=1*[n^2+(n-1)^2+n(n-1)]
=n^2+(n-1)^2+n^2-n
=2*n^2+(n-1)^2-n
2^3-1^3=2*2^2+1^2-2
3^3-2^3=2*3^2+2^2-3
4^3-3^3=2*4^2+3^2-4
......
n^3-(n-1)^3=2*n^2+(n-1)^2-n
各等式全相加
n^3-1^3=2*(2^2+3^2+...+n^2)+[1^2+2^2+...+(n-1)^2]-(2+3+4+...+n)
n^3-1=2*(1^2+2^2+3^2+...+n^2)-2+[1^2+2^2+...+(n-1)^2+n^2]-n^2-(2+3+4+...+n)
n^3-1=3*(1^2+2^2+3^2+...+n^2)-2-n^2-(1+2+3+...+n)+1
n^3-1=3(1^2+2^2+...+n^2)-1-n^2-n(n+1)/2
3(1^2+2^2+...+n^2)=n^3+n^2+n(n+1)/2=(n/2)(2n^2+2n+n+1)
=(n/2)(n+1)(2n+1)
1^2+2^2+3^2+...+n^2=n(n+1)(2n+1)/6
1^3+2^3+3^3+……+n^3=[n(n+1)/2]^2
(n+1)^4-n^4=[(n+1)^2+n^2][(n+1)^2-n^2]
=(2n^2+2n+1)(2n+1)
=4n^3+6n^2+4n+1
2^4-1^4=4*1^3+6*1^2+4*1+1
3^4-2^4=4*2^3+6*2^2+4*2+1
4^4-3^4=4*3^3+6*3^2+4*3+1
......
(n+1)^4-n^4=4*n^3+6*n^2+4*n+1
各式相加有
(n+1)^4-1=4*(1^3+2^3+3^3...+n^3)+6*(1^2+2^2+...+n^2)+4*(1+2+3+...+n)+n
4*(1^3+2^3+3^3+...+n^3)=(n+1)^4-1+6*[n(n+1)(2n+1)/6]+4*[(1+n)n/2]+n
=[n(n+1)]^2
1^3+2^3+...+n^3=[n(n+1)/2]^2
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<stack>
#include<queue>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#define eps 1e-8
#define zero(x) (((x>0?(x):-(x))-eps)
#define mem(a,b) memset(a,b,sizeof(a))
#define memmax(a) memset(a,0x3f,sizeof(a))
#define pfn printf("\n")
#define ll __int64
#define ull unsigned long long
#define sf(a) scanf("%d",&a)
#define sf64(a) scanf("%I64d",&a)
#define sf264(a,b) scanf("%I64d%I64d",&a,&b)
#define sf364(a,b,c) scanf("%I64d%I64d%I64d",&a,&b,&c)
#define sf2(a,b) scanf("%d%d",&a,&b)
#define sf3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sf4(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d)
#define sff(a) scanf("%f",&a)
#define sfs(a) scanf("%s",a)
#define sfs2(a,b) scanf("%s%s",a,b)
#define sfs3(a,b,c) scanf("%s%s%s",a,b,c)
#define sfd(a) scanf("%lf",&a)
#define sfd2(a,b) scanf("%lf%lf",&a,&b)
#define sfd3(a,b,c) scanf("%lf%lf%lf",&a,&b,&c)
#define sfd4(a,b,c,d) scanf("%lf%lf%lf%lf",&a,&b,&c,&d)
#define sfc(a) scanf("%c",&a)
#define ull unsigned long long
#define pp pair<int,int>
#define debug printf("***\n")
const double PI = acos(-1.0);
const double e = exp(1.0);
const int INF = 0x7fffffff;;
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> inline T Min(T a, T b) { return a < b ? a : b; }
template<class T> inline T Max(T a, T b) { return a > b ? a : b; }
bool cmpbig(int a, int b){ return a>b; }
bool cmpsmall(int a, int b){ return a<b; }
using namespace std;
int main()
{
// freopen("data.in","r",stdin);
ll n;
while(~sf64(n))
{
ll pos;
pos=(((n%10000*((n+1)%10000))/2)%10000*((n%10000*((n+1)%10000))/2)%10000)%10000;
if(pos<1000)
{
printf("0");
if(pos<100)
{
printf("0");
if(pos<10)
printf("0");
}
}
printf("%I64d\n",pos);
}
}