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 1007. Code Words

Why I got WA?
Posted by qwt 12 Feb 2002 07:01
var
  n,l:integer;
  s:array[0..1001] of 0..1;
  z:char;
procedure print(a,b:integer);
var
  i:integer;
begin
  for i:=a to b do write(s[i]);
end;
procedure work(l,n:integer);
var
  i,j,k:longint;
  a:array[0..1001] of integer;
begin
  fillchar(a,sizeof(a),0);k:=0;
  for i:=1 to l do begin
    inc(a[i],a[i-1]+ord(s[i])-ord('0'));
    if s[i]=1 then begin inc(k,i);k:=k mod (n+1);end;
  end;
  inc(k,n+1);
  if l<n then begin
    if k mod (n+1)=0 then begin print(1,l);writeln(0);exit;end;
    for i:=l downto 1 do
      if (k+a[l]-a[i-1]) mod (n+1)=0 then begin
        print(1,i-1);write(0);print(i,l);writeln;exit;
      end else
      if (k+a[l]-a[i-1]+i) mod (n+1)=0 then begin
        print(1,i-1);write(1);print(i,l);writeln;exit;
      end;
  end else
  if l=n then begin
    if k mod (n+1)=0 then begin
      print(1,l);writeln;exit;end;
    for i:=1 to n do
      if (s[i]=1)and( (k-i) mod (n+1)=0) then begin
        s[i]:=0;print(1,l);writeln;exit;
      end;
  end else
  begin
    for i:=1 to l do
      if (s[i]=1)and((k-a[l]+a[i+1]-i) mod (n+1)=0) then begin
        print(1,i-1);print(i+1,l);writeln;exit;
      end else
      if (s[i]=0)and((k-a[l]+a[i+1]) mod (n+1)=0) then begin
        print(1,i-1);print(i+1,l);writeln;exit;
      end;
  end;
end;

begin
  readln(n);
  while not(eof) do begin
    l:=0;
    fillchar(z,sizeof(z),0);
    while not(eoln) do begin
      inc(l);
      read(z);
      if z='1' then s[l]:=1;
    end;
    readln;
    work(l,n);
  end;
end.