ENG  RUSTimus Online Judge
Online Judge
Задачи
Авторы
Соревнования
О системе
Часто задаваемые вопросы
Новости сайта
Форум
Ссылки
Архив задач
Отправить на проверку
Состояние проверки
Руководство
Регистрация
Исправить данные
Рейтинг авторов
Текущее соревнование
Расписание
Прошедшие соревнования
Правила
вернуться в форум

Обсуждение задачи 1165. Subnumber

Could anyone try to find why does my program get WA?
Послано shitty.Mishka 20 дек 2001 15:24
Program Subnumber;
Const MaxLen=400;
Type TNum=Array[0..MaxLen] Of Integer;{0-sign of the number}
Procedure Add(A,B:TNum;Var C:TNum);   Forward;
Procedure Sub(A,B:TNum;Var C:TNum);   Forward;
Procedure Mul10(Pow:Integer;Var X:TNum); Forward;
Procedure Mulm10(Pow:Integer;Var X:TNum);Forward;
Procedure Mul(A,B:TNum;Var C:TNum);   Forward;
Procedure MulC(A:TNum;C:Integer;Var B:TNum);Forward;
Procedure InputLong(Var X:TNum);      Forward;
Procedure PrintLong(Var X:TNum);      Forward;
Function Len(X:TNum):Integer;         Forward;
Function CmpEqLns(A,B:TNum):Char;     Forward;
Function Cmp(A,B:TNum):Char;          Forward;
Procedure Dvd(A,B:TNum;Var C:TNum);   Forward;
Procedure InToTNum(K:Integer;Var R:TNum); Forward;
Procedure StToTNum(S:String;Var R:TNum);  Forward;
 Function Max(X,Y:Integer):Integer;
 Begin
  If X>Y Then Max:=X Else Max:=Y;
 End;
 Procedure Add;
  Var I,D                     :Integer;
 Begin
  D:=0;
  For I:=MaxLen DownTo MaxLen-Max(Len(A),Len(B)) Do Begin
   C[I]:=(A[I]+B[I]+D) Mod 10;
   D:=(A[I]+B[I]+D) Div 10;
  End;
 End;
 Procedure Mul10;
  Var I:Integer;
 Begin
  For I:=1 To MaxLen-Pow Do
   X[I]:=X[I+Pow];
  For I:=MaxLen+1-Pow To MaxLen Do
   X[I]:=0;
 End;
 Procedure Mulm10;
  Var I:Integer;
 Begin
  For I:=MaxLen DownTo Pow+1 Do
   X[I]:=X[I-Pow];
  For I:=1 To Pow Do
   X[I]:=0;
 End;
 Procedure Mul;
  Var I,J,D                  :Integer;
      T                      :TNum;
 Begin
  For I:=1 To MaxLen Do C[I]:=0;
  For I:=MaxLen DownTo MaxLen+1-Len(B) Do Begin
   For J:=1 To MaxLen Do T[J]:=0;
   D:=0;
   For J:=MaxLen DownTo 1 Do Begin
    T[J]:=(A[J]*B[I]+D) Mod 10;
    D:=(A[J]*B[I]+D) Div 10;
   End;
   Mul10(MaxLen-I,T);
   Add(T,C,C);
  End;
 End;
 Procedure MulC;
  Var i,j,d:Integer;
      t:TNum;
 Begin
  For I:=1 To MaxLen Do B[I]:=0;
  For J:=1 To MaxLen Do T[J]:=0;
  D:=0;
  For J:=MaxLen DownTo MaxLen-Len(A) Do Begin
   T[J]:=(A[J]*C+D) Mod 10;
   D:=(A[J]*C+D) Div 10;
  End;
  Add(T,B,B);
 End;
 Procedure InputLong;
  Var I                      :Integer;
      S                      :String;
      Cod                    :Integer;
 Begin
  Readln(S);
  If S[1]='-' Then Begin
   X[0]:=1;
   Delete(S,1,1);
  End;
  For I:=1 To Length(S) Do Val(S[I],X[MaxLen-Length(S)+I],Cod);
 End;
 Procedure PrintLong;
  Var I                      :Integer;
 Begin
  If Len(X)=0 Then
   Write(0)
  Else
   For I:=MaxLen+1-Len(X) To MaxLen Do
    Write(X[I]);
 End;
 Function Len;
  Var I                      :Integer;
 Begin
  Len:=0;
  I:=1;
  While ((X[I]=0) And (I<=MaxLen)) Do Inc(I);
  Len:=MaxLen+1-I;
 End;
 Function CmpEqLns;
  Var I                      :Integer;
      R                      :Char;
 Begin
  R:='=';
  I:=1;
  While ((I<=MaxLen) And (R='=')) Do Begin
   If A[I]>B[I] Then
    R:='>'
   Else If A[I]<B[I] Then
    R:='<';
   Inc(I);
  End;
  CmpEqLns:=R;
 End;
 Function Cmp;
 Begin
  If A[0]=1 Then
   If B[0]=1 Then Begin
    A[0]:=0;
    B[0]:=0;
    Cmp:='=';
    If Cmp(A,B)='<' Then
     Cmp:='>'
    Else If Cmp(A,B)='>' Then
     Cmp:='<'
   End Else
    Cmp:='<'
  Else
   If B[0]=1 Then
    Cmp:='>'
   Else
    If Len(A)>Len(B) Then Cmp:='>' Else
     If Len(A)<Len(B) Then Cmp:='<' Else
      Cmp:=CmpEqLns(A,B);
 End;
 Procedure Sub;
  Var I,D                    :Integer;
 Begin
  If A[0]=1 Then Begin
   A[0]:=1-A[0];
   B[0]:=1-B[0];
   Sub(A,B,C);
   C[0]:=1-C[0]
  End Else
   If Cmp(A,B)='<' Then Begin
    Sub(B,A,C);
    C[0]:=1-C[0];
   End Else
    If B[0]=1 Then Begin
     B[0]:=0;
     Add(A,B,C);
    End Else Begin
     D:=0;
     For I:=MaxLen DownTo MaxLen-Max(Len(A),Len(B)) Do Begin
      C[I]:=(A[I]-B[I]+10-D) Mod 10
Hope this'll help you !
Послано I have answers to all your questions :) 20 дек 2001 18:51
I'm sorry that i dont have time to look deeper in ur program , it's
quite complex :)
- There's a bug in function Len, "While ((X[I]=0) And (I<=MaxLen)) Do
Inc(I)" must be changed to "While ((I<=MaxLen) and (X[I]=0)) Do Inc
(I)" otherwise u'll get array bound exceeded (put $R+ in the first
line of ur program then submit it , u'll see that)
- I found a case which ur program gives WA : "09910", the correct
answer is 909,910,.. instead of 99,100,..

That's all :)
Good luck!
Thank you! But there's still something wrong. here's my new code.
Послано shitty.Mishka 20 дек 2001 20:29
Could you try to find a new test case please?

Program Subnumber;
Const MaxLen=205;
Var mt,dt:Array[0..100] Of Byte;
    st:Array[0..9,0..9] Of Byte;
Type TNum=Array[0..MaxLen] Of Integer;{0-sign of the number}
Procedure Add(A,B:TNum;Var C:TNum);   Forward;          {&#1057;&#1083;&#1086;&#1078;&#1077;&#1085;&#1080;&#1077;}
Procedure Sub(A,B:TNum;Var C:TNum);   Forward;
Procedure Mul10(Pow:Integer;Var X:TNum); Forward;          {&#1059;&#1084;&#1085;&#1086;&#1078;&#1077;&#1085;&#1080;&#1077;
&#1085;&#1072; 10 &#1074; &#1089;&#1090;&#1077;&#1087;&#1077;&#1085;&#1080;}
Procedure Mulm10(Pow:Integer;Var X:TNum);Forward;
Procedure Mul(A,B:TNum;Var C:TNum);   Forward;          {&#1059;&#1084;&#1085;&#1086;&#1078;&#1077;&#1085;&#1080;&#1077;}
Procedure MulC(A:TNum;C:Integer;Var B:TNum);Forward;       {&#1059;&#1084;&#1085;&#1086;&#1078;&#1077;&#1085;&#1080;&#1077;
&#1095;&#1080;&#1089;&#1083;&#1072; &#1085;&#1072; &#1094;&#1080;&#1092;&#1088;&#1091;}
Procedure InputLong(Var X:TNum);      Forward;          {&#1042;&#1074;&#1086;&#1076;}
Procedure PrintLong(Var X:TNum);      Forward;          {&#1042;&#1099;&#1074;&#1086;&#1076;}
Function Len(X:TNum):Integer;         Forward;          {&#1054;&#1087;&#1088;&#1077;&#1076;&#1077;&#1083;&#1077;&#1085;&#1080;&#1077;
&#1076;&#1083;&#1080;&#1085;&#1099;}
Function CmpEqLns(A,B:TNum):Char;     Forward;          {&#1057;&#1088;&#1072;&#1074;&#1085;&#1077;&#1085;&#1084;&#1077;
&#1095;&#1080;&#1089;&#1077;&#1083; &#1086;&#1076;&#1085;&#1086;&#1081; &#1076;&#1083;&#1080;&#1085;&#1099;}
Function Cmp(A,B:TNum):Char;          Forward;          {&#1057;&#1088;&#1072;&#1074;&#1085;&#1077;&#1085;&#1080;&#1077;
&#1095;&#1080;&#1089;&#1077;&#1083;}
Procedure Dvd(A,B:TNum;Var C:TNum);   Forward;
Procedure InToTNum(K:Integer;Var R:TNum); Forward;
Procedure StToTNum(S:String;Var R:TNum);  Forward;
 Function Max(X,Y:Integer):Integer;
 Begin
  If X>Y Then Max:=X Else Max:=Y;
 End;
 Procedure Add;
  Var I,D,v                     :Integer;
 Begin
  D:=0;
  For I:=MaxLen DownTo 1 Do Begin
   v:=st[A[I],B[I]]+D;
   C[I]:=mt[v];
   D:=dt[v];
  End;
 End;
 Procedure Mul10;
  Var I:Integer;
 Begin
  For I:=1 To MaxLen-Pow Do
   X[I]:=X[I+Pow];
  For I:=MaxLen+1-Pow To MaxLen Do
   X[I]:=0;
 End;
 Procedure Mulm10;
  Var I:Integer;
 Begin
  For I:=MaxLen DownTo Pow+1 Do
   X[I]:=X[I-Pow];
  For I:=1 To Pow Do
   X[I]:=0;
 End;
 Procedure Mul;
  Var I,J,D                  :Integer;
      T                      :TNum;
 Begin
  For I:=1 To MaxLen Do C[I]:=0;
  For I:=MaxLen DownTo MaxLen+1-Len(B) Do Begin
   For J:=1 To MaxLen Do T[J]:=0;
   D:=0;
   For J:=MaxLen DownTo 1 Do Begin
    T[J]:=(A[J]*B[I]+D) Mod 10;
    D:=(A[J]*B[I]+D) Div 10;
   End;
   Mul10(MaxLen-I,T);
   Add(T,C,C);
  End;
 End;
 Procedure MulC;
  Var i,j,d:Integer;
      t:TNum;
 Begin
  For I:=1 To MaxLen Do B[I]:=0;
  For J:=1 To MaxLen Do T[J]:=0;
  D:=0;
  For J:=MaxLen DownTo MaxLen-Len(A) Do Begin
   T[J]:=(A[J]*C+D) Mod 10;
   D:=(A[J]*C+D) Div 10;
  End;
  Add(T,B,B);
 End;
 Procedure InputLong;
  Var I                      :Integer;
      S                      :String;
      Cod                    :Integer;
 Begin
  Readln(S);
  If S[1]='-' Then Begin
   X[0]:=1;
   Delete(S,1,1);
  End;
  For I:=1 To Length(S) Do Val(S[I],X[MaxLen-Length(S)+I],Cod);
 End;
 Procedure PrintLong;
  Var I                      :Integer;
 Begin
  If Len(X)=0 Then
   Write(0)
  El
try this input : 0909 , the correct answer is 35251
Послано I have answers to all your questions :) 20 дек 2001 23:12