51nod 1073 Joseph Ring (mathematical recursion)

Source: Internet
Author: User
Tags time limit

1073 Joseph Ring Reference time limit: 1 seconds space limit: 131072 KB score: 0 Difficulty: Basic collection Attention N the person sits into a circle (numbered 1-n), from the 1th person starts to count, the number to K of the person out, the back of the person from 1 began counting off. Ask the number of the last remaining person. For example: N = 3,k = 2. Number 2nd, then number 1th, and the last one is number 3rd. Input

2 numbers n and K, which represent n individuals, count to K to dequeue. (2 <= N, K <= 10^6)
Output
The number of the last remaining person
Input example
3 2
Output example
3

I feel that maths problem is very difficult ...


The analysis process is this, assuming that the first round of elimination is numbered k-1 (numbering is starting from 0), then there will be a first round and the second round of mapping relationship.

K----------------->0

K+1-------------->1

...

N-1--------------->n-k-1

0------------------>n-k

...

K-2--------------->n-2 (the first round on the left and the second round on the right)

Set X as the number in the second round, according to the above mapping, there will be such a relationship (X+k)%n. The reason is that N is the remainder because it is from the second round n-1 personal mapping to the first round of n individuals.

And k=m%n, bring into the formula can get (x+m)%n.

And so pushed down, from the second round to the third round, it is (x+m)% (n-1), so recursion until the end.

Can think of the last round of time, there is only one person, then he is the winner, numbered 0.

So according to the above relationship, back, get the last round numbered 0 people in the first round of the number, is the answer.


#include <iostream>
#include <cstdio>
#include <cstring>
#define  max_n 1000005
using namespace std;
int main ()
{
    int n,k;
    int f[max_n];
    while (cin>>n>>k)
    {
        f[1]=0;
        for (int i=2;i<=n;i++)
        {
            f[i]= (f[i-1]+k)%i;
        }
        printf ("%d\n", f[n]+1);
    }
    return 0;
}


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.