## 392 - Polynomial Showdown

Moderator: Board moderators

shamim
A great helper
Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Bozeman, Montana, USA
It would be better if you mentioned the name or/and number of the problem.

afzaalakhtar
New poster
Posts: 1
Joined: Fri Jun 11, 2004 7:34 pm

### Polynomial Showdown plz help me

I've tried each and every input my code give me the correct result
but acm gives WA why?????

if anybody can help me...... my source code is below

// Programmed by Afzaal Akhtar
// Polynomial ShowDown ACM Problem 392
#include<iostream>
#include<sstream>

using namespace std;

const int MAX = 8;
int p[MAX];

string writepoly()

{
stringstream s;
int power;
bool started;

started = false;

for(power = MAX; power>=0;power--){
if (p[power] != 0){
if (started) {
if (p[power] < 0) {
s<<" - ";
p[power] = -p[power];
}
else
s<<" + ";
}
else{
started = true;
if (p[power] < 0){
s<<"-";
p[power] = -p[power];
}
}
if ((p[power] != 1) || (power == 0))
s<<p[power];
if (power>0)
s<<"x";
if (power>1)
s<<"^"<<power;
}
}
if(!started)
s<<"0";

s<<"\n";

return s.str();
}

int main()
{

while(!cin.eof()){
for(int i=MAX;i>=0;i--)
cin>>p;

cout<<writepoly();
}
return 0;
}

samueljj
New poster
Posts: 18
Joined: Fri Jul 18, 2003 5:24 am
midra.... probably the instead of -x in line 2 it will be - x..... i think so...

tahsin..
novice programmer

midra
Experienced poster
Posts: 119
Joined: Fri Feb 13, 2004 7:20 am
Contact:
thanks a lot!!
I fix this little things, but I am still WA!!
I really got stuck with this!
Here is my code: (I know it's a very ineficient program, but I am a newcomer in this field so I don't know any other way to do it)

[c]
#include <stdio.h>
#include <math.h>
int main()
{
int c[13]; /*Coeficients*/
int i; /*counter*/
int n; /*Numero para saber si ya hizo el 1er coef.*/

empieza:
while(scanf("%d%d%d%d%d%d%d%d%d",&c[9],&c[8],&c[7],&c[6],&c[5],&c[4],&c[3],&c[2],&c[1])==9)
{
n=0;
if (c[1]==0 && c[2]==1 && c[3]==0 && c[4]==0 && c[5]==0 && c[6]==0 && c[7]==0 && c[8]==0 && c[9]==0){
printf("x\n");
goto empieza;
}
else if (c[1]==0 && c[2]==-1 && c[3]==0 && c[4]==0 && c[5]==0 && c[6]==0 && c[7]==0 && c[8]==0 && c[9]==0){
printf("-x\n");
goto empieza;
}
else if (c[1]==-1 && c[2]==1 && c[3]==0 && c[4]==0 && c[5]==0 && c[6]==0 && c[7]==0 && c[8]==0 && c[9]==0){
printf("-x + 1\n");
goto empieza;
}
else if (c[1]==1 && c[2]==1 && c[3]==0 && c[4]==0 && c[5]==0 && c[6]==0 && c[7]==0 && c[8]==0 && c[9]==0){
printf("x + 1\n");
goto empieza;
}
else if (c[1]==-1 && c[2]==-1 && c[3]==0 && c[4]==0 && c[5]==0 && c[6]==0 && c[7]==0 && c[8]==0 && c[9]==0){
printf("-x - 1\n");
goto empieza;
}
else if (c[1]==1 && c[2]==-1 && c[3]==0 && c[4]==0 && c[5]==0 && c[6]==0 && c[7]==0 && c[8]==0 && c[9]==0){
printf("x - 1\n");
goto empieza;
}
for (i=9; i>=1; i--)
{
if (i==1 && n!=0)
{
if (c>0)
printf("+ %d",c);
else if (c<0)
printf("- %d",abs(c));
printf("\n");
goto empieza;
}
else if (i==1 && n==0)
{
printf("%d",c);
printf("\n");
goto empieza;
}

else if (c==0)
continue;
else if (i==2)
{
if (c>1)
printf("+ %dx ",c);
else if (c<-1)
printf("- %dx",abs(c));
else if (c[i]==-1)
printf("- x ");
else if (c[i]==1)
printf("+ x ");
}
else if (n==0 && abs(c[i])!=1)
{
printf("%dx^%d ",c[i],i-1);
n++;
}
else if (c[i]==-1 && n==0)
{
printf("- x^%d ",i-1);
n++;
}
else if (c[i]==1 && n==0)
{
printf("x^%d ",i-1);
n++;
}
else if (c[i]>1)
printf("+ %dx^%d ",c[i],i-1);
else if (c[i]<-1)
printf("- %dx^%d ",abs(c[i]),i-1);
else if (c[i]==1)
printf("+ x^%d ",i-1);
else if (c[i]==-1)
printf("- x^%d ",i-1);
}
}
return 0;
}
[/c]

PerHagen
New poster
Posts: 23
Joined: Thu Oct 14, 2004 1:45 am
Location: lima

### 392

hello

i have with W.A.
my cod is

include <stdio.h>
include <iostream.h>
int arreglo[9];
void main(void)
{
while(scanf("%d %d %d %d %d %d %d %d %d",
&arreglo[0],&arreglo[1],&arreglo[2],&arreglo[3],
&arreglo[4],&arreglo[5],&arreglo[6],&arreglo[7],
&arreglo[8])==9)

{ int i=0;
while (arreglo==0)i++;

if (i<9){(arreglo!=1 && arreglo!=-1 &&i!=8)?cout<<arreglo:cout<<"";
(arreglo==-1 && i!=8)?cout<<"-":cout<<"";
(i<8)?cout<<"x":cout<<"";
(i<7)?cout<<"^"<<(8-i):cout<<"";
(i==8 && arreglo>0 )?cout<<arreglo:cout<<"";
(i==8 && arreglo<0 )?cout<<arreglo:cout<<"";
}
i++;

while(i<9)
{
if(arreglo!=0){
(arreglo[i]>0)?cout<<" + ":cout<<" - ";
(arreglo[i]>0 && arreglo[i]!=1 && arreglo[i]!=-1&& i!=7 && i!=8)?cout<<arreglo[i]:cout<<"";
(arreglo[i]<0 && arreglo[i]!=1 && arreglo[i]!=-1&& i!=7 && i!=8)?cout<<(-1*arreglo[i]):cout<<"";
(i==7 && arreglo[i]>0 && arreglo[i]!=1 &&arreglo[i]!=-1)?cout<<arreglo[i]:cout<<"";
(i==7 && arreglo[i]<0 && arreglo[i]!=1 &&arreglo[i]!=-1)?cout<<(-1*arreglo[i]):cout<<"";
(i<8)?cout<<"x":cout<<"";
(i<7)?cout<<"^"<<(8-i):cout<<"";
(i==8 && arreglo[i]>0)?cout<<arreglo[i]:cout<<"";
(i==8 && arreglo[i]<0)?cout<<(-1*arreglo[i]):cout<<"";
}
i++;
}
cout<<endl;
}
}

help[/code][/list][/quote]
hello !

Sedefcho
A great helper
Posts: 374
Joined: Sun Jan 16, 2005 10:18 pm
Location: Bulgaria
It is mentioned in the Thread Name.

392-Anybody help me?

I would like to have some I/O for this Problem - 392.
Apparently I am missing some very subtle cases which
could cause problems.

Ghust_omega
Experienced poster
Posts: 115
Joined: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 + 6x^6 - x^4 + 6x^3 - 2x^2 - 2x + 7
7x^7 - 9x^4 - 9x^2
7x^6 + 6x^5 - 7x^4 - 7x^3 - 9x^2
x^6 + 8x^5 + 4x^4 + 9x^3 - 3x^2 + 6x + 5
6x^8 + 9x^7 - 5x^5 - 8x^4 + 8x^3 + 9x + 3
8x^8 + 9x^7 - 5x^6 + x^5 - x^4 + 6x^3 - 5x^2 + 8x + 3
4x^8 - 4x^7 + 4x^6 - 3x^4 - x^2 + 8x + 4
6x^8 + 9x^7 - 5x^6 + 7x^5 - x^4 + 3x^3 - 6x^2 + 7x + 4
``````
Hope it Helps
Keep posting

Sedefcho
A great helper
Posts: 374
Joined: Sun Jan 16, 2005 10:18 pm
Location: Bulgaria
Thank you, Ghust_omega.

help other people working on this one.

triker
New poster
Posts: 5
Joined: Tue Apr 19, 2005 9:56 am

### 392 Polynomial Showdown Plz Help

I got WA. I searched the forum and try inputs. It got the output right but still WA.

Here is my code

Code: Select all

``````#include <stdio.h>
#include <math.h>

int main()
{
const int MAX_CO=9;
int poly[MAX_CO];
int count=1,power;
int i;

while(scanf("%d%d%d%d%d%d%d%d%d",&poly[0],&poly[1],&poly[2],&poly[3],&poly[4],&poly[5],&poly[6],&poly[7],&poly[8])==9)
{
count = 1;
for(i=0;i<MAX_CO;i++)
{
power = MAX_CO - i -1;

/*last number which has no 'x' and no power*/
if(power == 0)
{
/*if it's the first number that appears (all number before are zero*/
if(count==1)
{
printf("%d",poly[i]);
continue;
}
/*if it's not the first number that appears*/
if(poly[i]>0) /*positive*/
{
printf(" + ");
printf("%d",poly[i]);
}
else if(poly[i]<0) /*negative*/
{
printf(" - ");
printf("%d",0-poly[i]);
}
continue;
}

/*first number that appears*/
if((count == 1)&&(poly[i] != 0))
{
if(poly[i]>1)
{
printf("%dx",poly[i],power);
}
else if(poly[i]<-1)
{
printf("%dx",poly[i],power);
}
else if(poly[i] == 1)
{
printf("x");
}
else if(poly[i] == -1)
{
printf("-x");
}

/*write out its power*/
if((power != 0)&&(power !=1))
{
printf("^%d",power);
}
count++;
continue;
}

/*second number and so on*/
if((count>1)&&(poly[i] != 0)&&(power !=0))
{
if(poly[i]>0)
{
printf(" + ");
}
else
{
printf(" - ");
}

if(poly[i]>1) /*positive*/
{
printf("%dx",poly[i]);
}
else if((poly[i] == 1)||(poly[i] == -1))
{
printf("x");
}
else if(poly[i]<-1) /*negative*/
{
printf("%dx",0-poly[i]);
}
else {}
}

/*write out the power*/
if((power != 0)&&(power !=1)&&(poly[i] != 0))
{
printf("^%d",power);
}

}
}
return 0;
}``````
plz help

Ali Arman Tamal
Learning poster
Posts: 76
Joined: Sat Jan 15, 2005 5:04 pm
Location: Dhaka
Contact:

### 392 more test cases

input:
-1 -1 -1 -1 -1 -1 -1 -1 -1
-1 0 0 0 0 0 0 0 0
-1 0 0 0 0 0 0 0 -1
0 0 0 0 0 0 0 0 0
-999 0 -1 999 0 1 999 1 0 -2
4 5 -1 8 0 -2 -2 -3
1 1 1 1 1 1 1 1 1
-0 -0 -0 -0 -0 -0 -0 -0 -0

output:
-x^8 - x^7 - x^6 - x^5 - x^4 - x^3 - x^2 - x - 1
-x^8
-x^8 - 1
0
-999x^8 - x^6 + 999x^5 + x^3 + 999x^2 + x
-2x^8 + 4x^7 + 5x^6 - x^5 + 8x^4 - 2x^2 - 2x - 3
x^8 + x^7 + x^6 + x^5 + x^4 + x^3 + x^2 + x + 1
0
-10x^4
10x^4

hope it helps

Niaz
Learning poster
Posts: 77
Joined: Fri Dec 17, 2004 11:06 am
Location: East West University, Dhaka, Bangladesh
Contact:
I am trying to solve this problem for the last 3 years ! But getting again and again WA !!! I have tried all the test inputs and outputs given in the board. My WA code can produce correct output for all the given inputs. What can I do now ? Is there any one who can help me ? If any interested person wants to watch my code, I can send him mine. But, plz help me. I am suffering from such an easy problem. This is disgusting.

- Niaz
Please join The ACM Solver Group at Yahoo
http://groups.yahoo.com/group/acm_solver/

Niaz
Learning poster
Posts: 77
Joined: Fri Dec 17, 2004 11:06 am
Location: East West University, Dhaka, Bangladesh
Contact:
Here is my code. Plz have a look.

Code: Select all

``````Got Accepted. Thanks to Roby.
``````
Last edited by Niaz on Tue Dec 06, 2005 7:04 pm, edited 1 time in total.
Please join The ACM Solver Group at Yahoo
http://groups.yahoo.com/group/acm_solver/

Niaz
Learning poster
Posts: 77
Joined: Fri Dec 17, 2004 11:06 am
Location: East West University, Dhaka, Bangladesh
Contact:
Can anyone help me ? Getting WA for the last 3 years !!!

Code: Select all

``````#include<stdio.h>
int main()
{
int coeff[10];
int i,k;
while(scanf("%d",&i)==1)
{
coeff[8]=i;
for(i=0;i<8;i++)
scanf("%d",&coeff[7-i]);

k=0;
for(i=8;i>1;i--)
{
if(coeff[i]>0)
{
if(k==1)
printf(" + ");
if(coeff[i]!=1)
printf("%dx^%d",coeff[i],i);
else
printf("x^%d",i);
k=1;
}
else if(coeff[i]<0)
{

if(k==0)
printf("-");
else
printf(" - ");

coeff[i]=coeff[i]*(-1);
if(coeff[i]!=1)
printf("%dx^%d",coeff[i],i);
else
printf("x^%d",i);
k=1;
}
}

if(coeff[1]!=0)
{
i=1;
if(coeff[i]>0)
{
if(k==1)
printf(" + ");
if(coeff[i]!=1)
printf("%dx",coeff[i]);
else
printf("x");
k=1;
}
else if(coeff[i]<0)
{
if(k==0)
printf("-");
else
printf(" - ");
coeff[i]=coeff[i]*(-1);
if(coeff[i]!=1)
printf("%dx",coeff[i]);
printf("x");
k=1;
}
}

if(coeff[0]!=0)
{
i=0;
if(coeff[i]>0)
{
if(k==1)
printf(" + ");
printf("%d",coeff[i]);
k=1;
}
else if(coeff[i]<0)
{
if(k==0)
printf("-");
else
printf(" - ");
coeff[i]=coeff[i]*(-1);
printf("%d",coeff[i]);
k=1;
}
}
if(k==0)
printf("0");
printf("\n");
}
return 0;
}
``````
Please join The ACM Solver Group at Yahoo
http://groups.yahoo.com/group/acm_solver/

Niaz
Learning poster
Posts: 77
Joined: Fri Dec 17, 2004 11:06 am
Location: East West University, Dhaka, Bangladesh
Contact:
got AC.
Please join The ACM Solver Group at Yahoo
http://groups.yahoo.com/group/acm_solver/

Roby
Experienced poster
Posts: 101
Joined: Wed May 04, 2005 4:33 pm
Location: Tangerang, Banten, Indonesia
Contact:
Your program give wrong result if I test with these:

Code: Select all

``````0 0 0 1 22 -333 0 1 -1
-1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 -7 30 66
0 0 0 0 0 0 1 -3 0
0 0 0 0 0 -1 1 3 -1
-5 0 0 0 -243 0 0 0 -9
-0 -1 -1 -1 -1 -1 -1 -1 -1
-1999999999 -978456 0 0 0 0 56 -89 8
0 0 0 0 0 0 0 0 -1
0 0 0 0 0 0 0 0  1
0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 -1 0
0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 -1 1
0 0 0 0 0 0 0 -1 -1
0 0 0 0 0 0 0 1 -1
0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0
0 0 -1 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 2
0 0 0 0 0 0 0 0 -10
0 0 0 0 0 0 0 10 0
0 0 0 0 0 0 0 -2 0
0 0 0 0 0 0 0 10 -2
``````
My accepted code give these result:

Code: Select all

``````x^5 + 22x^4 - 333x^3 + x - 1
-x^8
0
-7x^2 + 30x + 66
x^2 - 3x
-x^3 + x^2 + 3x - 1
-5x^8 - 243x^4 - 9
-x^7 - x^6 - x^5 - x^4 - x^3 - x^2 - x - 1
27649x^8 + 4584x^7 + 56x^2 - 89x + 8
-1
1
x
-x
x + 1
-x + 1
-x - 1
x - 1
1
x^8
x^7
-x^6
2x^8
2
-10
10x
-2x
10x - 2
``````
And these one are yours:

Code: Select all

``````x^5 + 22x^4 - 333x^3 + x - 1
-x^8
0
-7x^2 + 30x + 66
x^2 - 3xx
-x^3 + x^2 + 3x - 1
-5x^8 - 243x^4 - 9
-x^7 - x^6 - x^5 - x^4 - x^3 - x^2 - x - 1
27649x^8 + 4584x^7 + 56x^2 - 89xx + 8
-1
1
x
-x
x + 1
-x + 1
-x - 1
x - 1
1
x^8
x^7
-x^6
2x^8
2
-10
10x
-2xx
10x - 2
``````
Be careful! This problem is output related! Even if you just add one space after the line, you'll get WA!
Hope it helps