ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1032. Find a Multiple

Why I get WA?I know maybe my algorithm is not the best one.
Posted by lyj_george 1 Oct 2002 09:23
if input is
1
4
What should I output?
1
4
OR
1
1
?

and my program:
const
  max = 10000;
var
  num : array [1..max] of integer;
  o1 , o2 : array [0..max-1] of boolean;
  pre : array [0..max-1] of integer;
  i , j , n : integer;
procedure init;
begin
  readln(n);
  for i := 1 to n do
    readln(num[i]);
  fillchar(o1,sizeof(o1),0);
  o1[0] := true;
  fillchar(pre,sizeof(pre),0);
end;
procedure out;
var
  link , tot : integer;
  sz : array [1..max] of integer;
begin
  fillchar(sz,sizeof(sz),0);
  tot := 0;
  link := 0;
  repeat
    inc(tot);
    sz[tot] := pre[link];
    link := (link + n - num[pre[link]] mod n) mod n;
  until link = 0;
  writeln(tot);
  for link := 1 to tot do
    writeln(sz[link]);
  halt;
end;



procedure solve;
begin
  for i := 1 to n do begin
    o2 := o1;
    for j := 0 to n - 1 do
      if (o1[j]) and (pre[(j+num[i] mod n) mod n] = 0) then begin
        o2[(j+num[i] mod n) mod n] := true;
        pre[(j+num[i] mod n) mod n] := i;
      end;
    if (o2[j] = true) and (pre[0]<>0) then out;
    o1 := o2;
  end;
end;


begin
  init;
  solve;
  writeln(0);
end.

I think it's O(n^2),maybe TIME LIMIT EXCEED,but NOW I GET WA!
You may think an O(n) algo (+)
Posted by Miguel Angel 1 Oct 2002 09:37
as they give you N values the next expresion:
Sum(first i elements) mod N
gives you in worst case, N different values, one of them of course is
equal to zero.



> const
>   max = 10000;
> var
>   num : array [1..max] of integer;
>   o1 , o2 : array [0..max-1] of boolean;
>   pre : array [0..max-1] of integer;
>   i , j , n : integer;
> procedure init;
> begin
>   readln(n);
>   for i := 1 to n do
>     readln(num[i]);
>   fillchar(o1,sizeof(o1),0);
>   o1[0] := true;
>   fillchar(pre,sizeof(pre),0);
> end;
> procedure out;
> var
>   link , tot : integer;
>   sz : array [1..max] of integer;
> begin
>   fillchar(sz,sizeof(sz),0);
>   tot := 0;
>   link := 0;
>   repeat
>     inc(tot);
>     sz[tot] := pre[link];
>     link := (link + n - num[pre[link]] mod n) mod n;
>   until link = 0;
>   writeln(tot);
>   for link := 1 to tot do
>     writeln(sz[link]);
>   halt;
> end;
>
>
>
> procedure solve;
> begin
>   for i := 1 to n do begin
>     o2 := o1;
>     for j := 0 to n - 1 do
>       if (o1[j]) and (pre[(j+num[i] mod n) mod n] = 0) then begin
>         o2[(j+num[i] mod n) mod n] := true;
>         pre[(j+num[i] mod n) mod n] := i;
>       end;
>     if (o2[j] = true) and (pre[0]<>0) then out;
>     o1 := o2;
>   end;
> end;
>
>
> begin
>   init;
>   solve;
>   writeln(0);
> end.
>
> I think it's O(n^2),maybe TIME LIMIT EXCEED,but NOW I GET WA!
>