126 The Errant Physicist
Posted: Tue Oct 28, 2003 12:34 am
by Farid Ahmadov
Hi people. Can anyone help me. What is wrong with my program? I get always correct answers on my tests. I've done this problem even in USACO - ace.delos.com. But here I get WA.
[pascal]program The_Errant_Physicist;
const
num: array[0..200] of string[3] = ('0','1','2','3','4','5','6','7','8','9','10',
'11','12','13','14','15','16','17','18','19','20',
'21','22','23','24','25','26','27','28','29','30',
'31','32','33','34','35','36','37','38','39','40',
'41','42','43','44','45','46','47','48','49','50',
'51','52','53','54','55','56','57','58','59','60',
'61','62','63','64','65','66','67','68','69','70',
'71','72','73','74','75','76','77','78','79','80',
'81','82','83','84','85','86','87','88','89','90',
'91','92','93','94','95','96','97','98','99','100',
'101','102','103','104','105','106','107','108','109','110',
'111','112','113','114','115','116','117','118','119','120',
'121','122','123','124','125','126','127','128','129','130',
'131','132','133','134','135','136','137','138','139','140',
'141','142','143','144','145','146','147','148','149','150',
'151','152','153','154','155','156','157','158','159','160',
'161','162','163','164','165','166','167','168','169','170',
'171','172','173','174','175','176','177','178','179','180',
'181','182','183','184','185','186','187','188','189','190',
'191','192','193','194','195','196','197','198','199','200');
type
Term = record
c: Integer;
e: array['x'..'y'] of Integer;
end;
Polynomial = record
n: Integer;
t: array[1..200] of Term;
end;
var
u,v: string;
pa,pb,pc: Polynomial;
procedure getterms(a: string; var b: Polynomial);
var
i,l,s,c,e: Integer;
t: Char;
begin
l:=length(a);
b.n:=0; i:=1;
while i<=l do
begin
inc(b.n);
s:=1;
while a in ['+','-'] do
begin
if a='-' then s:=-s;
inc(i);
end;
c:=0;
if a in ['0'..'9'] then
begin
while (a in ['0'..'9'])and(i<=l) do
begin
c:=c*10+ord(a)-48;
inc(i);
end;
c:=c*s;
end else
begin
c:=s;
end;
b.t[b.n].c:=c;
while (a in ['x','y'])and(i<=l) do
begin
t:=a;
inc(i);
if (a in ['0'..'9'])and(i<=l) then
begin
e:=0;
while (a in ['0'..'9'])and(i<=l) do
begin
e:=e*10+ord(a)-48;
inc(i);
end;
end else e:=1;
b.t[b.n].e[t]:=e;
end;
end;
end;
procedure swap(var a,b: Term);
var
c: Term;
begin
c:=a; a:=b; b:=c;
end;
procedure multiply(a,b: Polynomial; var c: Polynomial);
var
i,j,k: Byte;
t: Term;
begin
c.n:=0;
for i:=1 to a.n do
for j:=1 to b.n do
begin
fillchar(t,sizeof(t),0);
t.c:=a.t[i].c*b.t[j].c;
t.e['x']:=a.t[i].e['x']+b.t[j].e['x'];
t.e['y']:=a.t[i].e['y']+b.t[j].e['y'];
k:=c.n;
while (k>0)and((t.e['x']<>c.t[k].e['x'])or(t.e['y']<>c.t[k].e['y'])) do dec(k);
if k>0 then
begin
c.t[k].c:=c.t[k].c+t.c;
end else
begin
inc(c.n);
k:=c.n;
c.t[k]:=t;
while k>1 do
begin
if (c.t[k].e['x']>c.t[k-1].e['x'])or((c.t[k].e['x']=c.t[k-1].e['x'])and(c.t[k].e['y']<c.t[k-1].e['y'])) then swap(c.t[k],c.t[k-1]) else break;
dec(k);
end;
end;
end;
end;
procedure len(x: Integer; var l: Byte);
begin
if x=0 then l:=0 else
if x<10 then l:=1 else
if x<100 then l:=2 else
if x<1000 then l:=3 else
if x<10000 then l:=4 else l:=5;
end;
procedure print(a: Polynomial);
var
i,j,k,l,l1,l2: Byte;
s,z,u: string;
begin
s:='';
z:='';
k:=1;
while (a.t[k].c=0)and(k<=a.n) do inc(k);
if k<=a.n then
for i:=1 to a.n do
if a.t[i].c<>0 then
begin
if (i=k)and(a.t[i].c<0) then
begin
s:=s+' ';
z:=z+'-';
end else
if i>k then
begin
s:=s+' ';
if a.t[i].c>0 then z:=z+' + ' else z:=z+' - ';
end;
if (abs(a.t[i].c)>1)or((abs(a.t[i].c)=1)and(a.t[i].e['x']=0)and(a.t[i].e['y']=0)) then
begin
len(abs(a.t[i].c),l);
for j:=1 to l do s:=s+' ';
str(abs(a.t[i].c),u);
z:=z+u;
end;
if a.t[i].e['x']>0 then
begin
s:=s+' ';
z:=z+'x';
if a.t[i].e['x']>1 then
begin
s:=s+num[a.t[i].e['x']];
len(a.t[i].e['x'],l);
for j:=1 to l do z:=z+' ';
end;
end;
if a.t[i].e['y']>0 then
begin
s:=s+' ';
z:=z+'y';
if a.t[i].e['y']>1 then
begin
s:=s+num[a.t[i].e['y']];
len(a.t[i].e['y'],l);
for j:=1 to l do z:=z+' ';
end;
end;
end;
l1:=length(s);
while (s[l1]=' ')and(l1>0) do
begin
delete(s,l1,1);
dec(l1);
end;
l2:=length(z);
while (z[l2]=' ')and(l2>0) do
begin
delete(z,l2,1);
dec(l2);
end;
if l1<l2 then
begin
writeln(s,'':l2-l1);
writeln(z);
end else
begin
writeln(s);
writeln(z,'':l1-l2);
end;
end;
begin
{$IFNDEF ONLINE_JUDGE}
assign(input,'126.in'); reset(input);
assign(output,'126.out'); rewrite(output);
{$ENDIF}
readln(u);
while u='' do readln(u);
while u[1]<>'#' do
begin
readln(v);
while v='' do readln(v);
fillchar(pa,sizeof(pa),0);
fillchar(pb,sizeof(pb),0);
fillchar(pc,sizeof(pc),0);
getterms(u,pa);
getterms(v,pb);
multiply(pa,pb,pc);
print(pc);
readln(u);
while u='' do readln(u);
end;
{$IFNDEF ONLINE_JUDGE}
close(input); close(output);
{$ENDIF}
end.[/pascal]
Thanks in advance.