## 498 - Polly the Polynomial

Moderator: Board moderators

sohag144
New poster
Posts: 14
Joined: Mon Feb 27, 2006 4:12 pm
Contact:

### 498-why WA

I checked all i/o that i got from previous topic.
But i m getting WA.Can anyone help?

Code: Select all

``````code deleted after got ac
``````
Last edited by sohag144 on Sun Jul 02, 2006 8:34 pm, edited 1 time in total.
the LA-Z-BOy
Learning poster
Posts: 94
Joined: Wed Jul 31, 2002 12:44 pm
Contact:
char word[MAX][10];
seems too small here... get something like
char word[MAX][20];
Also you would get Presentation Error on this, because you are printing extra space at end of each line.
Greetings.
Istiaque Ahmed [the LA-Z-BOy]
sohag144
New poster
Posts: 14
Joined: Mon Feb 27, 2006 4:12 pm
Contact:
Thank you again.
You help me a lot.got AC.
ALEXANDRIA_2
New poster
Posts: 10
Joined: Tue Nov 07, 2006 2:03 pm
Location: Jakarta
I can't understand why I keep gettin WA all the time...
can anyone tell me what's wrong with my code...

Code: Select all

``````#include<stdlib.h>
#include<string.h>
#include<stdio.h>

long int A[100];
long int B[100];
int in;

int tes(char line[],int w){
long int i,len,isneg=1,f=0,sum=0,T;
in=0;
len=strlen(line);
for(i=0;i<len;i++)
{
if(line[i]=='-') isneg=0;
else
if(line[i]!=' ')
{
f=1;
T=line[i]-'0';
sum*=10;
sum+=T;
}
else
{
if(f)
{
if(!isneg) sum*=-1;
if(!w)A[in]=sum;
else B[in]=sum;
sum=0;
in++;
isneg=1;
f=0;
}
}
}

if(!isneg) sum*=-1;
if(!w)A[in]=sum;
else B[in]=sum;
in++;

return in;
}

long int sum(long int angka,long int pang){
int i;
if(!pang) return 1;
for(i=1;i<pang;i++)
{
angka*=angka;
}
return angka;
}

void main(){
char line[128];
int m,n;
long int T=0,X,Angka,k,pang,i,t=0,ctr=0;
while(gets(line))
{
if(ctr) printf("\n");
else ctr=1;
t=0;
m=tes(line,0);
gets(line);
n=tes(line,1);

for(k=0;k<n;k++)
{
Angka=B[k];
pang=m-1;
for(i=0;i<m;i++)
{
X=sum(Angka,pang);
X*=A[i];
T+=X;
pang--;
}
if(!t) {printf("%ld",T);t=1;}
else printf(" %ld",T);
T=0;
}
T=in=0;
memset(A,0,sizeof(A));
memset(B,0,sizeof(B));

}
}
``````
I've passed all the input given in this thread..
and the output is correct....
Thanks...
Impossible is nothing
S.M.ferdous
New poster
Posts: 13
Joined: Fri Nov 03, 2006 2:53 pm
Contact:

### How to get even number line input???(498,10268)

Hello to all

I have solved 498 problem but my problem in getting input.

I am not understanding how to accept an even number of lines of text.
Another puzzling matter is that I can't correctly implement the breaking condition.

I need some help.plz help me.
loghiyan
New poster
Posts: 6
Joined: Fri Dec 22, 2006 6:19 pm
Try this way. It might do the trick ( haven't seen the problem statement :-p) :

Code: Select all

``````      char line1[200],line2[200];
int n;
char *c,*d;
while (gets(line1) && gets(line2)){
c=strtok(line1," \n");
while (c){
n=atoi(c);
cout<<n<<endl;
c=strtok(NULL," \n");
}
d=strtok(line2," \n");
while (d){
n=atoi(d);
cout<<n<<endl;
d=strtok(NULL," \n");
}
}
``````
Hope that helps.
Freyr
New poster
Posts: 5
Joined: Sun Apr 22, 2007 8:47 pm
Contact:
Yeah, does anyone have any tricky test data for this? I've made loads of different possible problems, and my program solves them all, but still gets WA.

*shrugs*
Freyr
New poster
Posts: 5
Joined: Sun Apr 22, 2007 8:47 pm
Contact:
Nevermind, it was a presentation error, but it came up as wrong answer.

For anyone else who may need test data...

Code: Select all

``````-1 1 -11 1 -1
1 -1 11 -11
45 0 23 0 21
500 2333 3213 1
``````

Code: Select all

``````-11 -15 -14631 -17315
2812505750021 1333126955170913 4795737875394753 89
``````
Double works.

Edit: Removed certain test cases as they don't appear in the judge's input file.
Freeman
New poster
Posts: 1
Joined: Sun Jul 22, 2007 11:17 am

### 498 TLE ! Need Help

I don't know if it's because of cin.getline
My Sample Output is correct.

This is my code (a bit long):

Code: Select all

``````#include<iostream>
#include<cmath>
#include<string>
using namespace std;
int main()
{
int i,k,len,ct,now;
int a[10000],b[10000],neg,xd=0;
char z[10000],zx[10000];
double sum,total;

while( cin.getline(z,10000) )
{
ct=0; total=0;  neg=0;  now=0;
len = strlen(z);

for (i=len-1;i>=0;i--)
{
if (z[i]>=48 && z[i]<=57)
{
total += int ( (z[i]-48)*pow(10.0,ct) );
ct++;
}
else if (z[i]=='-')
total *= -1;
if (z[i]==' ' || i==0)
{
a[now] = int(total);
xd++;  // control of XD
ct=0;
total=0;
neg=0;
now++;
}

} //for
// cout << a[0] << " " << a[1] << " " << a[2] << endl;
int test=0;
now=0; total=0; neg=1; ct=0; sum=0;// neg 1 = +  -1= -

cin.getline(zx,10000);

len = strlen(zx);
for (k=len-1;k>=0;k--)
{

if (zx[k]=='-')
total *= -1;
else if (zx[k]>=48 && zx[k]<=57)
{
total += (zx[k]-48)*pow(10.0,ct);
// cout << "total=" << total << endl;
ct++;
}

if (zx[k]==' ' || k==0)
{
for(i=0;i<xd;i++)
{
sum += a[i]*pow( total,i );
// cout << "sum=" << sum << endl;
}
b[test]=sum;

sum=0;
total=0;
now++;
ct=0;
test++;

if (zx[k]==10)
break;
}

} // for
for (i=test-1;i>=0;i--)
{
cout << b[i];
if (i!=0)
cout << " ";
}
cout << endl;

} //while

return 0;
}
``````
Can someone tell me what's wrong with it? Thks.
Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Contact:
Ami ekhono shopno dekhi...
HomePage
vkubushyn
New poster
Posts: 6
Joined: Fri Oct 26, 2007 11:34 pm
Location: Las Vegas, NV

Hey, my code for this problem seems impeccable to me, and it's solved all the examples on this forum correctly, and yet I still get WA. Can anyone tell me what's wrong with my code? I tried printing a space after each line, not printing an ending blank line, and other possible PE problems... Thanks.

Code: Select all

``````#include <iostream>
#include <math.h>
#include <vector>
#include <string>
#include <iomanip>
#include <sstream>
using namespace std;

int main() {
cout << fixed << setprecision(0);
vector<double> coeff;
string s;
stringstream* sin;
double n, count, sum;
getline(cin, s);
while(!cin.eof()) {
sin = new stringstream(s);
while(*sin >> n)
coeff.push_back(n);

getline(cin, s);
if(!cin.eof()) {
sin = new stringstream(s);
*sin >> n;
while((*sin).good()) {
sum = 0; count = 0;
for(int i = coeff.size()-1; i >= 0; i--) {
sum += coeff[i]*pow(n, count);
count++;
}
cout << sum << " ";
*sin >> n;
}
coeff.clear();
getline(cin, s);
cout << endl;
}
}
return 0;
}
``````
jjtse
Learning poster
Posts: 80
Joined: Mon Aug 22, 2005 7:32 pm
Contact:
Does anyone know if there's a limit on 'n' and 'm'?
Samiul
New poster
Posts: 36
Joined: Thu Dec 13, 2007 3:01 pm
1000 will be enough for both n and m.
thomas1016
New poster
Posts: 19
Joined: Mon May 29, 2006 4:12 pm

### istringstream works !!!!

part of my code
I finally got AC

Code: Select all

``````istringstream iss;
``````
istringstream works !!!!
thanks a lot

Code: Select all

``````#include<iostream>
#include<sstream>
#include<string>

using namespace std;
int pol(int,string);

void polf(string,string);

int main(){
int j,sum;
string po,k;

while(1){

if (!getline(cin,po)) break;

if (!getline(cin,k)) break;
polf(k,po);
}
//system("pause");
return 0;
}``````
maruf
New poster
Posts: 17
Joined: Sat May 24, 2008 6:00 pm

### Re: 498 TLE

For each pair of lines, your program should evaluate the polynomial for all the values of x
what is meant by "evaluating" here??plz help........
lives for eternity......