## 498 - Polly the Polynomial

Moderator: Board moderators

FlyDeath
Learning poster
Posts: 73
Joined: Wed Jan 02, 2002 2:00 am
Location: Taiwan
498 seems to be an easy problem,but i get WA all the time.I believe there is nothing wrong with my code.Maybe i misunderstood the problem or there is some special input.Anyway,here is my code:

Code: Select all

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

void main()
{
long long cn;
char temp;
long long tmp;
long num=0;
int flag=0;
int i;
long long ans=0;
while(scanf("%lld%c",&tmp,&temp)!=EOF)
{
if(flag==0)
{
cn[num]=tmp;
num++;
if(temp=='n')
flag=1;
}
else if(flag==1)
{
ans=0;
for(i=0;i<num;i++)
ans=ans*tmp+cn[i];
printf("%lld",ans);
if(temp=='n')
{
printf("n");
flag=0;
num=0;
}
else
flag=2;
}
else if(flag==2)
{
ans=0;
for(i=0;i<num;i++)
ans=ans*tmp+cn[i];
printf(" %lld",ans);
if(temp=='n')
{
printf("n");
flag=0;
num=0;
}
}
}
}
``````

Even
Learning poster
Posts: 75
Joined: Thu Nov 22, 2001 2:00 am
Location: Taiwan
Maybe it contains a space after the last number of a line...

such as...

1 -1
7 6 -1

FlyDeath
Learning poster
Posts: 73
Joined: Wed Jan 02, 2002 2:00 am
Location: Taiwan
Ya,I've fixed it.But i still get WA.I think that's not the problem.

Even
Learning poster
Posts: 75
Joined: Thu Nov 22, 2001 2:00 am
Location: Taiwan
and you source code now ?....

--
P.S. I only use "int"...

xenon
Learning poster
Posts: 100
Joined: Fri May 24, 2002 10:35 am
Location: Scheveningen, Holland

### 498 - made to make a pascal-programmer feel inferior?

...or am I loosing my mind. For all I know the following code should work perfectly:
[pascal]program p498(input,output);

var
factor:array[1..100] of integer;
factors:integer;
x,result,i,powerofx:integer;
first:boolean;
begin
while not eof(input) do begin
factors:=0;
while not eoln(input) do begin
inc(factors);
end;
first:=true;
while not eoln(input) do begin
result:=factor[factors];
powerofx:=x;
for i:=(factors-1) downto 1 do begin
result:=result+factor*powerofx;
powerofx:=powerofx*x;
end;
if first then first:=false else write(' ');
write(result);
end;
writeln;
end;
end.[/pascal]
But I keep getting WAs.
Is this one of the cases where the input is littered whith nasty spaces at places you don't expect them, just to make life miserable, or is there something completely wrong with my program?
It strikes me that not many Pascal-programmers solved this problem. We don't have scanf() functions that return -1 to indicate failure when reading things, so this kind of joke is specially cruel to us...
Please help me out xenon
Learning poster
Posts: 100
Joined: Fri May 24, 2002 10:35 am
Location: Scheveningen, Holland

### solved it

It was indeed the case that the input was not well formed. Wrote my own input routine, and got accepted.
I don't think it's fair though. I can't see any added value in making the problems harder this way; once you have written your own input routine, the gain in programming experience is nil. And the code gets ugly. For this relative simple problem the number of lines is tripled, the readability is severely decreased.
If the ability to cope with dirty input should be tested, make a specific problem for that, but don't spil it over other problems.
But that is only my personal opinion, of course...

liusu
New poster
Posts: 22
Joined: Thu Aug 01, 2002 10:26 am

### why WA?

I Think there is no error,but still WAs,why?plz help me [pascal]var
s:array[1..10000000] of integer;
p,a:integer;

function f(a,b:integer):double;
begin
if b=0 then
f:=1
else f:=a*f(a,b-1);
end;

procedure done(a:integer);
var
re:double;
i:integer;
begin
re:=0;
for i:=1 to p do
re:=re+s*f(a,p-i);
write(re:0:0,' ');
end;

begin
while not eof do
begin
p:=0;
while not eoln do
begin
inc(p);
end;
while not eoln do
begin
done(a);
end;
writeln;

end;
end.[/pascal]

titid_gede
Experienced poster
Posts: 187
Joined: Wed Dec 11, 2002 2:03 pm
Location: Mount Papandayan, Garut

### 498 - Polly the Polynomial

i didnt find this problem is difficult, but however i'm still get wrong answer after 5 - 6 submitting. are there special cases?
here is my code :
[c]

---- edited ---
[/c]

i've posted the wrong source code (that code will get compile error).
i've tested code below, and it was OK. dunno where the mistake was [c]
/*@JUDGE_ID: XXXXX 498 C*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

#define MAX 100

int data[MAX], jumdata = 0, mark = 0;

int getline (void) {
char ch;
int t = 0, isneg = 1;

ch = getchar();
while (1) {
if (ch == EOF) return 0;
if (ch == '\n')
break;
if (isdigit (ch))
t = (t*10) + (ch-'0');
else if (ch == '-')
isneg = -1;
else {
data[jumdata++] = isneg*t;
t = 0;
isneg = 1;
}
ch = getchar();
}
data[jumdata++] = isneg*t;
return 1;
}

int getvalue(int *val) {
char ch;
int t = 0, isneg = 1;

ch = getchar();
while (!isdigit (ch)) {
if (ch == '\n') {
mark = 1;
return 0;
} else if (ch == '-')
isneg = -1;
ch = getchar();
}
while (isdigit (ch)) {
t = (t * 10) + (ch - '0');
ch = getchar();
}
if (ch == '\n') mark = 1;
*val = isneg * t;
return 1;
}

double proccess (int value) {
double result = 0, s;
int i, j = 0;

for (i = jumdata-1; i >= 0; i--) {
s = pow (value, i);
result += data[j] * s;
j++;
}
return result;
}

int main() {
double result;
char ch;
int value;

while (1) {
if (!getline()) break;
while (!mark) {
getvalue (&value);
result = proccess (value);
printf ("%0.lf ", result);
}
printf ("\n");
jumdata = 0; mark = 0;
}
return 0;
}

[/c]  Eric
Learning poster
Posts: 83
Joined: Wed Sep 11, 2002 6:28 pm
Location: Hong Kong
I think I should have handle the spaces and blank lines.
However, I still go on to grt WA.
Can anyone help me?

Red Scorpion
Experienced poster
Posts: 192
Joined: Sat Nov 30, 2002 5:14 am
Hi!

I have solved this problem using integer, not double.

input:
10002 1001 102
3 0 1 0 -1

Output:
93123 102 11105 102 9103

Good Luck!

titid_gede
Experienced poster
Posts: 187
Joined: Wed Dec 11, 2002 2:03 pm
Location: Mount Papandayan, Garut
i've change my code, and use only integers and i've got the same result as yours. but still WA. Red Scorpion
Experienced poster
Posts: 192
Joined: Sat Nov 30, 2002 5:14 am
Hi!

titid_gede
Experienced poster
Posts: 187
Joined: Wed Dec 11, 2002 2:03 pm
Location: Mount Papandayan, Garut
here is my code :
[c]
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define MAX 100

int data[MAX], jumdata = 0, mark = 0;

int getline (void) {
char ch;
int t = 0, isneg = 1;

ch = getchar();
while (1) {
if (ch == EOF) return 0;
if (ch == '\n')
break;
if (isdigit (ch))
t = (t*10) + (ch-'0');
else if (ch == '-')
isneg = -1;
else {
data[jumdata++] = isneg*t;
t = 0;
isneg = 1;
}
ch = getchar();
}
data[jumdata++] = isneg*t;
return 1;
}

int getvalue(int *val) {
char ch;
int t = 0, isneg = 1;

ch = getchar();
while (!isdigit (ch)) {
if (ch == '\n') {
mark = 1;
return 0;
} else if (ch == '-')
isneg = -1;
ch = getchar();
}
while (isdigit (ch)) {
t = (t * 10) + (ch - '0');
ch = getchar();
}
if (ch == '\n') mark = 1;
*val = isneg * t;
return 1;
}

int power (int a, int b) {
int ct, res;

if (b == 0) return 1;
res = 1;
for (ct = 1; ct <= b; ct++)
res *= a;
return res;
}

int proccess (int value) {
int result = 0, s;
int i, j = 0;

for (i = jumdata-1; i >= 0; i--) {
s = power (value, i);
result += data[j] * s;
j++;
}
return result;
}

int main() {
char ch;
int value, result, i;

while (1) {
if (!getline()) break;
while (!mark) {
if (getvalue (&value)) {
result = proccess (value);
printf ("%d ", result);
}
}
printf ("\n");
jumdata = 0; mark = 0;
}
return 0;
}

[/c]

Red Scorpion
Experienced poster
Posts: 192
Joined: Sat Nov 30, 2002 5:14 am
Hi!

First Glance at your code, it seems alright,
but there's still some mistake.

Consider this input:

Code: Select all

``````input:
10   2    3
11   12   -2

Output:
1235 1467 39``````
Your code become wrong when there exist space.
Hope this helps.     titid_gede
Experienced poster
Posts: 187
Joined: Wed Dec 11, 2002 2:03 pm
Location: Mount Papandayan, Garut
that's the problem i've never realized. how stupid i am. thank you very much. got AC now   Kalo mau kaya, buat apa sekolah?