10302  Summation of Polynomials
Moderator: Board moderators
10302  Summation of Polynomials
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]
[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]
Hint
gcc (which is what UVA uses) has a "long long" data type which supports integers up to 2^64. So does Java's "long".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.
Formula
1^3 + 2^3 + 3^3 + ... ... ... +n^3
Sn=(n*(n+1)/2)^2
Sn=(n*(n+1)/2)^2
Formula using the question hints
I worked on the helper formulas given in the task and i came up with the following formula:
Sn = ((N1)*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.
Sn = ((N1)*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
I think this question can just use the long double.=>(%Lf)
it can solve it very quickly.
it can solve it very quickly.
10302 again
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]
[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]

 New poster
 Posts: 21
 Joined: Sun Jan 19, 2003 4:01 pm
 Location: Hong Kong
10302 why WA??
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]
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]

 New poster
 Posts: 21
 Joined: Sun Jan 19, 2003 4:01 pm
 Location: Hong Kong