hdu 2114 Calculate S(n)(找规律的题目,比赛的时候没做出来)

1、http://acm.hdu.edu.cn/showproblem.php?pid=2114

2、题目大意:

给定一个数字n,求出S(n)=13+23  +33  +......+n3,输出S(n)的后四位,一开始还以为打表呢,弄了好长时间发现根本就不对,到比赛结束都没做出来,现在看网上代码,才知道这道题目是有规律的,S(n)=(1+2+3+……+n)^2,这样交上去仍然是不对的,需要注意的是1+2+3+……+n是等差数列,可以用公式计算,for循环果断超时

3、题目:

Calculate S(n)

Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7130    Accepted Submission(s): 2637


Problem Description

Calculate S(n).

S(n)=1 3+2 3  +3 3  +......+n 3  .
 


 

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
 


 

Author

天邪
 


 

Source

HDU 2007-10  Programming Contest_WarmUp

 


 

Recommend

威士忌   |   We have carefully selected several similar problems for you:   2115  2117  2113  2116  2111 

 

4、AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
    __int64 n;
    __int64 sum,ans;
    __int64 s[5];
    while(scanf("%I64d",&n)!=EOF)
    {
        sum=0;
        sum=(n*(1+n))/2%10000;
        ans=(sum*sum)%10000;
        int k=0;
        memset(s,0,sizeof(s));
        while(ans)
        {
            if(k==4)
            break;
            s[k++]=ans%10;
            ans/=10;
        }
        for(int i=3;i>=0;i--)
        printf("%I64d",s[i]);
        printf("\n");
    }
    return 0;
}


 

你可能感兴趣的:(hdu 2114 Calculate S(n)(找规律的题目,比赛的时候没做出来))