The survivor label = (((((0+m1%2)+m2)%3+m3)%4 +m4)%5+m5)%6....
Examples:
Player : [0..5]
If m=3,
{0,1,2,3,4,5}
=>{0,1,3,4,5} - 2
=>{0,1,3,4} - 5
=>{0,1,4} - 3
=>{0,1} - 4
=>{0} - 1
Saved=(((((0+3)%2+3)%3+3)%4+3)%5+3)%6
= 1->1->0->3->0
Player : [0..4]
If m=3,
{0,1,2,3,4}
==>{0,1,3,4} -2
==>{0,1,3} -4
==>{1,3} - 0
==>{3} - 1
Saved=((((0+3)%2+3)%3+3)%4+3)%5
= 1->1->0->3
Player : [0..3]
If m=3,
{0,1,2,3}
==>{0,1,3} -2
==>{0,3} - 1
==>{0} - 3
Saved=(((0+3)%2+3)%3+3)%4
= 1->1->0
Player : [0..2]
If m=3,
{0,1,2}
==>{0,1} -2
==>{1} - 0
Saved=((0+3)%2+3)%3
= 1->1
Player : [0..1]
If m=3,
{0,1}
==>{0,1} -0
==>{1}
Saved=(0+3)%2
= 1
Player : [0]
No need to play!
Saved= 0
But, Why?
====================
Proof.
If there is n people, last killed guy is X, (X+m)%n will be the next guy to be killed.
If there is n people, killed guy is Y, (Y-m)% (n) will be the prev guy to be killed.
and all label after >= (Y-m)%(n) will be +1, then the last guy labelled as (Y-m)%n;
Ok, we do the process backward.
If the saviour guy is 0. add dummy 1.
(0-m)%2 will be the prev guy to be kill => (0-3)%2 =-3%1=1.
If we enforce prev guy to be zero, all other guys need to advance to position (?+m)%2, ?={0,1}
0->1
Then we add this new prev guy as '0'
1->0
The total displacement of the surviour = 0+m%2 = 1.
Result:{0,1}
Then in next round, added a dummy 2.
(0-m)%3 will be prev guy to be killed = -3%3=0
If we let this guy to be zero, all guys need to advance to position (?+m)%3, ?={0,1,2}
1->1
2->2 (dummy)
Then we add this new guy as '0'.
0->0;
The total displacement of the surviour = ((0+m%2)+m)%3
Result:{0,1,2}
Then in next round, added a dummy 3.
(0-m)%4 will be prev guy to be killed = -3%4=1
If we let this guy to be zero, all guys need to advance to position (?+m)%4, ?={0,1,2,3}
0->3
2->1
3->2 (dummy)
Then we add this new guy as '0'.
1->0;
The total displacement of the surviour = (((0+m%2)+m)%3+m)%4
Result:{0,1,2,3}
Then in next round, added a dummy 4.
(0-m)%5 will be prev guy to be killed = -3%5=2
If we let this guy to be zero, all guys need to advance to position (?+m)%5, ?={0,1,2,3,4}
0->3
1->4
3->1
4->2 (dummy)
Then we add this new guy as '0'.
2->0;
The total displacement of the surviour = ((((0+m%2)+m)%3+m)%4+m)%5
Result:{0,1,2,3,4}
Then in next round, added a dummy 5.
(0-m)%6 will be prev guy to be killed = -3%6=3
If we let this guy to be zero, all guys need to advance to position (?+m)%6, ?={0,1,2,3,4,5}
0->3
1->4
2->5
4->1
5->2(dummy)
Then we add this new guy as '0'.
3->0;
The total displacement of the surviour = (((((0+m%2)+m)%3+m)%4 +m)%5+m)%6
Result:{0,1,2,3,4,5}
* If m is changing in each round, the equation will be:
The total displacement of the surviour = (((((0+m1%2)+m2)%3+m3)%4 +m4)%5+m5)%6.....
Showing posts with label Algorithm. Show all posts
Showing posts with label Algorithm. Show all posts
Friday, March 9, 2012
Algorithm study: Josephus Problem
Labels:
Algorithm,
algorithms,
competitive,
dynamic programming,
Explaination,
Joseph,
Josephus,
Problem,
programming,
uva
Thursday, March 1, 2012
Algorithm : 1D Maxsum (Kadane algorithm with resulted sequence size >= 1)
// Written by Kevin C. Wong
// @JEngineTeam 2/9/2012
// This function is designed to solve uva108 maximum sum problem with dynamically programming.
// You can extend this to solve the 2D array in O(n^3).
// The original Kadane algorithm will return empty sequence if all array values are negative.
// We just return a sequence with single min value.
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
int max1d(int a[], int n, int &x1, int &x2)
{
int max=0;
int umax=a[0],ui=-1;
int here=0;
int c=0;
x1=x2=-1;
rep(i,0,n-1)
{
if(a[i]>=umax){umax=a[i];ui=i;};
c+=a[i];
if(c<0){here=i+1;c=0;};
if(c>max){max=c;x1=here;x2=i;};
}
if(x1==-1){x1=ui;x2=ui;return umax;}
return max;
}
Labels:
1D,
Algorithm,
algorithms,
C++,
dynamic programming,
Kadane algorithm,
Maxsum,
programming,
sequence,
size,
topcoder,
uva,
uva 108
Subscribe to:
Posts (Atom)