Joseph Ring Mathematical Solution F (n,k) = (f (n-1,k) +k)%n Formula explained

Source: Internet
Author: User

Question: There are n individuals standing into the ring from 1 to count, reported K people to die, and then the next person reported 1, asked when you are the first few can survive?

This article mainly explains F (n,k) = (f (n-1,k) +k)%n What does this formula mean?

Although the formula is used mathematically but initially I will manually simulate the process which is meaningful and very helpful to understand

First, let's look at something.

n=2, k=3

A b

We first use manpower to count a B a very good a dead

Now try it again n=2 k=4

A b

Manpower: A B A b very good B dead

n=2 k=5

Human a B A B a very good a dead

n=2 k=9999999

Manpower: t_t

For a B count to K dead its practical k%2 can quickly know who's going to die

Why?

A b

Set K=9 AB Altogether there are two so 9%2=1 meaning is the number of abab, counting after the last complete cycle and counting 1 (count to a) this is the meaning of modulus%

Now we're still using manual simulation n=11 k=3

This time, instead of the alphabet, the numbers are returned directly to the survivor's number (the difference between the first and the number of the spoiler note)

1 2 3 4 5 6 7 8 9 10 11

The 1th dead k%n is the third one to die, because we re counting 1 from the next, which means the dead next (number 4) is the beginning of the new ring, the last (number 3) is the end of the new ring

4 5 6 7 8 9 10 11 1 2 (This is the new ring)

Here's the rule, and I'm going to write it straight to the survivor.

1 2 3 4 5 6 7 8 9 10 11
4 5 6 7 8 9 10 11 1 2
7 8 9 10 11 1 2 4 5
10 11 1 2 4 5 7 8
2 4 5 7 8 10 11
7 8 10 11 2 4
11 2 4 7 8
7 8 11 2
2 7 8
2 7
7

When the last survivor is 7

------The end of the flower------

F (n,k) = (f (n-1,k) +k)%n What exactly does this formula describe? (How to use?)

Consider each row of the table above as an array this formula describes the next survivor in the next round of the subscript position

F (n,k) the calculated value is that there are n number of people K dead the last surviving person's subscript position is a few

It's obviously a recursive equation that requires a bottom-up operation.

At the bottom is F (1,k) F (1,k) =0 that means there's only one person when a survivor's subscript is 0 (nonsense-) Number 7

Up

F (2,k) = (f (1,k) +k)%n in n=11 k=3 is f (2,3) = (f (1,3) +3)%2=3%2=1

In the case of only two survivors the subscript position in this round array is 1 (look at the table above the subscript position is 1 number is 7)

Up

F (3,3) = (f (2,3) +3)%3=4%3=1

In the case of only three survivors the subscript position in this round array is 1 (look at the table above the subscript position is 1 number is 7)

F (4,3) = (f (3,3) +3)%4=4%4=0

In the case of only three survivors the subscript position in this round array is 0 (look at the table above the subscript position is 0 number is 7)

.

.

F (11,3) = (f (10,3) +3)%11=6%11=6 (look at the table above the first row subscript position is 6 number is 7)

It's easy for us to write according to the formula

1 int p=0; //  2for (int i=2; i<=n;i++)34      p= (p+k)%5 }

P is the subscript of the first-round survivor we asked for.

And in the first round no one died (tears.) The relationship between the number and subscript is number = Subscript +1 (array is stored starting from 0)

So the complete function code is

int Cir (int n,int  k) {    int p=0;      for (int i=2; i<=n;i++)    {        P= (p+k)%i;    }     return p+1;}

So why is this formula correct?

When we kill a man and form a new ring, it essentially falls off the person and moves the entire ring forward. K-bit

1 2 3 4 5 6 7 8 9 10 11

first round minus 3.

1 2 4 5 6 7 8 9 10 11

We're starting from the next 1, which means the dead next (number 4) is the beginning of the new ring (number 3) is the end of the new ring is to put the number 4 forward K (3) to make him a new ring start

1 2 3 4 5 6 7 8 9 10 11
1 2 3 4 5 6 7 8 9 10 11 1 2 3

Let's reiterate that f (n,k) = (f (n-1,k) +k)%n describes the change in the subscript position

The subscript position of the number 1 is 0 0 forward 3 the subscript position becomes -3-3+11=8 so the number 0 in the N-wheel subscript position 1 in the n-1 wheel becomes 8 in the subscript position.

If there is also a formula, it is probably F (n-1,k) =f (n,k)-k>=0?f (n,k)-k:f (n,k)-k+n;

Then it is clear that the subscript position of the nth wheel is calculated from the subscript position of the n-1 wheel f (n,k) =f (n-1,k) +k>=n?f (n-1,k) +k%n:f (n-1,k) +k (%n makes the subscript of F (n,k) within the range of N)

F (n,k) = (f (n-1,k) +k)%n

Above

The point is that F (n,k) describes the change in the position of the subscript.

Joseph Ring Mathematical Solution F (n,k) = (f (n-1,k) +k)%n Formula explained

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.