• Loading...

Problem 1: Circular Elimination

Points:

Small Case: 10 Answer: 106

Large Case: 20 Answer: 88999997

N numbers (1 to N) are arranged in a circular fashion. Starting from kth(<N), every second number is eliminated from the circle. You need to determine the xth(<N) number which is removed.

Small Case, N=1000, x=555, k=888.
large case, N=999999999, x=55555, k=88888888.


Solution:

This problem is commonly known as Extended Josephus Problem
The Reursive formula for the case when counting starts from 1 is given by:


J(n,x) = 1, if n=1 and x=1
         2, if n>1 and x=1
         1, if n,z>1 and J(n-1,x-1)=n-1
         J(n-1,x-1)+2, if J(n-1,x-1)<=n-2

Now, for the case when counting starts from k, we just shift kth value to 1st, and find kth number in circle.

Reference: Armin Shams-Baragh, Formulating The Extended Josephus Problem


Code:


#include <stdio.h>

long long Josephus(long long n, long long x)
{
  if(n==1 && x==1)
    return 1;
  if(n>1 && x==1)
    return 2;
  long long res = Josephus(n-1,x-1);
  if(res==(n-1))
    return 1;
  if(res <= n-2)
    return res+2;  
}

int main()
{
  long long n,x,k,res;
  scanf("%lld%lld%lld",&n,&x,&k);
  res = Josephus(n,x)+k-1;
  printf("%lld\n",res>n?res-n:res);
}

Submit your solution

You need to be logged in to submit a solution.

discussions


antalgStart number
Q:If k=4, does that mean:Remove 4, skip 5, remove 6 or Skip 4, remove 5, skip 6, remove 7?
A:It means Skip 4, remove 5, skip 6, remove 7?
anjan2question unclear
Q:if N=10 and k=5 then what is the first eleminated number?
A:If we start counting from 5, 6 is the second number, so 6 is eliminated first
codepravclarify
Q:suppose 1 to 10,then for x=5,k=8.answer=2 or not?
A:No. Eliminatio order is: 1st 9, 2nd 1, 3rd 3, 4th 5, 5th 7
gmunkhbaatarmnCount
Q:if N=10; k=5; x=1 then answer is: 5? or 7?
A:answer is 6. for numbers 1,2,3,4,5,6,7,8,9,10, if we start couting from k=5, x=1 elimination is 6
akash0604que
Q:kth term is taken as 0th term or 1st term to consider the 2nd number?
A:kth term is taken as first term
porshQuestion Unclear
Q:What is x,k?
A:xth number is the number removed in xth elimination. k is the number in the circle. For example if N=10 and k=5, we start elimination with couting starting from number 5.

post a question

Loading...

Problem 1
Problem 2
Problem 3
Problem 4
Problem 5 (bonus) Expired
Problem 1
Problem 2
Problem 3
Problem 4 (bonus) Expired
Top Scorers
rng_58 235
Anton_Lunyov 225
tourist 190
Silence 180
cedricl2 175
cgy4ever 175
DrKorbin 165
Koen 155
bbbasic 150
lunae_2 150
Submissions
Problem 1
10 pt
not submitted
98
submissions

19
correct (19 %)



25 pt
not submitted
2
submissions

1
correct (50 %)


Problem 2
5 pt
not submitted
95
submissions

38
correct (40 %)



10 pt
not submitted
19
submissions

15
correct (78 %)


Problem 3
5 pt
not submitted
153
submissions

117
correct (76 %)



15 pt
not submitted
44
submissions

25
correct (56 %)


Problem 4
10 pt
not submitted
152
submissions

54
correct (35 %)



15 pt
not submitted
23
submissions

10
correct (43 %)


Problem 5
10 pt
not submitted
36
submissions

28
correct (77 %)



20 pt
not submitted
6
submissions

2
correct (33 %)


Problem 6
5 pt
not submitted
41
submissions

29
correct (70 %)



10 pt
not submitted
15
submissions

14
correct (93 %)


Problem 7
5 pt
not submitted
38
submissions

17
correct (44 %)



10 pt
not submitted
16
submissions

15
correct (93 %)


Problem 8
5 pt
not submitted
127
submissions

104
correct (81 %)



15 pt
not submitted
47
submissions

34
correct (72 %)


Problem 9
5 pt
not submitted
86
submissions

35
correct (40 %)



15 pt
not submitted
16
submissions

15
correct (93 %)


Problem 10
5 pt
not submitted
45
submissions

23
correct (51 %)



15 pt
not submitted
15
submissions

14
correct (93 %)


Problem 11
5 pt
not submitted
81
submissions

27
correct (33 %)



15 pt
not submitted
8
submissions

3
correct (37 %)


Problem 12
5 pt
not submitted
104
submissions

88
correct (84 %)



10 pt
not submitted
31
submissions

21
correct (67 %)