All about problems in Volume CII. If there is a thread about your problem, please use it. If not, create one with its number in the subject.
Moderator: Board moderators
by @ce » Tue Jun 19, 2012 9:01 pm
Getting WA...plzz help
- Code: Select all
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
using namespace std;
int arr[100000000];
main()
{
string strx,str;
while(getline(cin, strx))
{
stringstream ss(strx);
long long int x;
ss>>x;
getline(cin, str);
stringstream in(str);
int n = 0;
while(in >> arr[n])
n++;
long long int poly = 0;
for(int i = 0;i<n ; i++)
poly = poly + (n-i-1)*arr[i]*pow(x,n-i-2.0);
printf("%lld\n", poly);
}
}
-@ce
-

@ce
- Learning poster
-
- Posts: 66
- Joined: Mon May 28, 2012 8:46 am
- Location: Ranchi, India
by brianfry713 » Tue Jun 19, 2012 11:25 pm
pow() returns a double and could lead to precision errors.
-
brianfry713
- Guru
-
- Posts: 1861
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
by @ce » Thu Jun 21, 2012 6:48 pm
Removing pow() gives TLE
- Code: Select all
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
using namespace std;
int arr[100000000];
long long int power(long long int x, long long int n)
{
if(n == 1)
return x;
else if(n == 0)
return 1;
else if(n&1)
return power(x,n/2)*power(x,n/2+1);
else
return power(x,n/2)*power(x,n/2);
}
main()
{
string strx,str;
while(getline(cin, strx))
{
stringstream ss(strx);
long long int x;
ss>>x;
getline(cin, str);
stringstream in(str);
int n = 0;
while(in >> arr[n])
n++;
long long int poly = 0;
for(int i = 0;i<n ; i++)
poly = poly + (n-i-1)*arr[i]*power(x,n-i-2);
printf("%lld\n", poly);
}
}
-@ce
-

@ce
- Learning poster
-
- Posts: 66
- Joined: Mon May 28, 2012 8:46 am
- Location: Ranchi, India
by brianfry713 » Thu Jun 21, 2012 9:06 pm
-
brianfry713
- Guru
-
- Posts: 1861
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Return to Volume CII
Who is online
Users browsing this forum: No registered users and 1 guest