ENG  RUS Timus Online Judge Online Judge
Problems
Authors
Online contests
Site news
Webboard
Problem set
Submit solution
Judge status
Guide
Register
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

## Discussion of Problem 1032. Find a Multiple

---> Very Simple Way with O(N) and pigeonhole proving! <---
Posted by Locomotive 19 Dec 2002 13:14
As you know i can prove that there is 1<=i<j<=n that:
( a[i]+a[i+1]+...+a[j-1]+a[j] ) mod n =0
because as box-principle(or pigeonhole) if we define
b[i]:=a[i] mod n
and b:=0;

we see b[x] es should be in range [0..n-1] but they are n+1 b(b to
b[n])
and it means there is two b (like (b[i],b[j]) that hey are equal
b[i]=b[j]
so
we see:
P1=a+a+a+..a[j]=l*n+b[j]
and
P2=a+a+a+..a[i]=k*n+b[i]
so if we calculate p1-p2 we see:
P3=a[j]+a[j-1]+a[j-2]+..a[i+2]+a[i+1]=
(l-k)*n+b[j]-b[i]
and we knew b[i]=b[j] so
P3=(l-k)*n ----> p3 mod n =0!!!!
so we should calcualte all b(s) and find I and J
so:
(with this algorithm we dont need to read all numbers!)
here is my code:

Var
n,i,k               :integer;
a                   :array[0..10000] of integer;
m                   :array[0.. 9999] of integer;
begin
fillchar(m,sizeof(m),-1);
m:=0; k:=0;
repeat
inc(a);
k:=(k+a[a]) mod n;
if m[k]=-1 then
m[k]:=a
else
begin
writeln(a-m[k]);
for i:=m[k]+1 to a do
writeln(a[i]);
break;
end;
until false;
end.

~~~~~~~~~~~~~~~~~~~~~~
Please tell me if you dont understand it at all.
Sincerely
Aidin_n7@hotmail.com

Re: ---> Very Simple Way with O(N) and pigeonhole proving! <---
Posted by Sid 18 Apr 2005 22:53
Cool It's great idea. But i think b[i]=(a+...+a[i])%n;

Edited by author 19.04.2005 14:19
Re: ---> Very Simple Way with O(N) and pigeonhole proving! <---
Posted by Machvi 24 Feb 2007 18:28
Sid wrote 18 April 2005 22:53
Cool It's great idea. But i think b[i]=(a+...+a[i])%n;

right!!!
Re: ---> Very Simple Way with O(N) and pigeonhole proving! <---
Posted by Machvi 24 Feb 2007 18:29

Edited by author 24.02.2007 18:30
Re: ---> Very Simple Way with O(N) and pigeonhole proving! <---
Posted by iverson_49 28 Mar 2007 17:41
that's it
Re: ---> Very Simple Way with O(N) and pigeonhole proving! <---
Posted by qwe (Dmitry) 5 Sep 2008 17:46
Thank you so so much ))
Re: ---> Very Simple Way with O(N) and pigeonhole proving! <---
Posted by grandvic 22 Apr 2011 03:47
Respect!
Re: ---> Very Simple Way with O(N) and pigeonhole proving! <---
Posted by jlcastrillon 19 Aug 2011 07:37
amazing, really amazing.