Page 1 of 2
10302 - Summation of Polynomials
Posted: Thu Jun 27, 2002 6:08 pm
by mido
I get WA with this code...any tips :
[cpp]
#include <iostream.h>
#include <stdio.h>
#include <math.h>
int
num,
i;
float sum;
void main()
{
while (!cin.eof() && cin>>num)
{
sum=0;
for (i=0;i<=num;i++)
sum+=pow(i,3);
printf("%.0lf\n",sum);
}
}
[/cpp]
Posted: Thu Jun 27, 2002 6:29 pm
by 10153EN
Clearly, there should be value overflow~
Posted: Thu Jun 27, 2002 7:03 pm
by mido
Well a friend just mentioned that any number type can only be accurate upto some length after which there will be some zeroes inputted automatically. Oh well.
Hint
Posted: Fri Jun 28, 2002 10:18 pm
by Joe Smith
mido wrote:Well a friend just mentioned that any number type can only be accurate upto some length after which there will be some zeroes inputted automatically. Oh well.
gcc (which is what UVA uses) has a "long long" data type which supports integers up to 2^64. So does Java's "long".
Formula
Posted: Sun Jul 21, 2002 12:29 pm
by popel
1^3 + 2^3 + 3^3 + ... ... ... +n^3
Sn=(n*(n+1)/2)^2

Formula using the question hints
Posted: Mon Oct 07, 2002 9:22 pm
by scythe
I worked on the helper formulas given in the task and i came up with the following formula:
Sn = ((N-1)*N*(N+1)*(N+2) + 2*N*(N+1)) / 4
which is actually an uglier form of your formula
I got AC using big numbers.
Re: Formula using the question hints
Posted: Tue Dec 03, 2002 9:04 pm
by supermin
I think this question can just use the long double.=>(%Lf)
it can solve it very quickly.

Posted: Tue Dec 03, 2002 10:15 pm
by Larry
long long works, and also gets you AC with good time.. =)
10302 again
Posted: Wed Feb 05, 2003 3:09 pm
by mido
Anything wrong with this:
[cpp]
#include <iostream.h>
#include <stdio.h>
#include <math.h>
long long
num,
i,j;
long long sum;
void main()
{
while (!cin.eof() && cin>>num && !cin.fail())
{
sum=0;
for (i=0;i<=num;i++)
{
long temp=1;
for (j=0;j<3;j++)
temp*=i;
sum+=temp;
}
cout<<sum<<endl;
}
}
[/cpp]
Posted: Sun Feb 09, 2003 11:38 am
by shamim
Your program seems to run ok.
But you might consider using a formula for the sum of Nth cube instead of using the iteration:
for (i=0;i<=num;i++)
{
long temp=1;
for (j=0;j<3;j++)
temp*=i;
sum+=temp;
}
the formula is :
(N*(N+1)/2)^2
10302 why WA??
Posted: Sat May 31, 2003 4:40 am
by Chow Wai Chung
This problem seem very easy, and i use (n*(n+1)/2)^2 to calculate the answer, but i get a WA....
Would anyone tell me what is my mistake??
[c]
#include <stdio.h>
#include <math.h>
int main()
{
long long result,n;
while(scanf("%lld",&n)!=EOF)
{
result=(long long)pow(n*(n+1)/2,2);
printf("%lld\n",result);
}
return 0;
}
[/c]
Posted: Sat May 31, 2003 5:59 am
by turuthok
Your formula looks great and you might want to try x = 50,000 and check your result using calculator. Most likely it is an overflow or precision problem.
-turuthok-
Posted: Sat May 31, 2003 5:22 pm
by IIUC GOLD
Use the following
result = n * n * (n + 1) * (n + 1) /4;
instead of
result=(long long)pow(n*(n+1)/2,2);
Posted: Mon Jun 02, 2003 1:45 am
by Chow Wai Chung
Thank you to turuthok and IIUC GOLD
I solved this problem by using n * n * (n + 1) * (n + 1) /4, may be i lost something when cast the pow() to long long before.
Posted: Sat Jul 05, 2003 3:34 pm
by mido
Got AC long time ago...who would've thought you needed for the temp variable....
