ACM: Special topic in number Theory (3)--Joseph's question

Source: Internet
Author: User


(P.S: Joseph was used to the problem of the chain-list simulation, today found a more efficient method, educated ... )

Title Description:

small Hi and small Ho's class are in the election of monitor, they decided to choose the monitor by a special way.

First n candidates in a circle, numbered 0. N-1. Then randomly selected a number k, and the No. 0 candidate began to count from 1 to K, the number of N-1th candidates, and again starting from 0. When someone reported to K, the man was eliminated from the circle. The next person starts to count back from 1.

In other words, the number of K per newspaper, will be eliminated one person. So after the N-1 round count, the circle only left 1 people, this person as a new monitor.

As an example, if there are 5 candidates, k=3:

     initial 0:0 1 2 3 4 
     count off from No. 0, 1th time 2nd, check-in 3
         1:0 1-3 4   //0 1 2, 2nd candidates to eliminate  
     count off from 3rd, 2nd time No. 0, check in 3
 & nbsp;        2:-1 3 4     //3 4 0, 0 candidate People out
     count off from 1th, 3rd time is 4th, check in. 3
         3:1 3-       //1 3 4,   4 candidate eliminated
      from 1th, 4th is 1th, report 3
         4 :-3         //1 3 1, 1th candidate eliminated
    
    topics require a given number of people n and K values To find the number of the person who was the last to be elected monitor.

  Answer:
The most primitive way to do this is to use a ring-linked list to simulate the number of all candidates, and when a candidate is eliminated, the corresponding node is removed from the linked list until only one node is left in the entire list. The time complexity of such an algorithm is O (NK), the efficiency is low, and if n is large, it will occupy a large amount of memory space, this is unrealistic.
Here are two recursive-based solutions:

• Recursion Method (1):
Set F (x) for a given K value case, when there are X candidates, the number of the last selected person. Obviously:
F (1) = 0 (when there are only 1 candidates, he is the last person to be elected).
When x>1, the value of f (x) can be calculated using the following recursive formula:
f (x) = (k + f (x-1))% n.
the formula proves as follows: If there are n candidates, then the first candidate to be eliminated must be a candidate with a number of k-1, such as:
after the elimination of the candidate numbered k-1, there are still n-1 candidates, at this time the n-1 personal re-numbering, the candidate numbered K to the number 0, the candidate numbered k+1 changed to number 1 ... The candidate numbered n-1 was replaced by the number n-k-1, then the candidate numbered 0 was changed to the number n-k, and the candidate with the number 1 changed to n-k+1 ... Candidates numbered k-2 are changed to numbered n-2. such as:
     at this point, assuming that the number of the last person to whom the n-1 candidate was known, the number of the last person to be selected when the number is converted to the old number as a new number is at the time of the N candidate.
    can be found: in the range of mod n, the difference between old and new numbers is that the old number adds a K value to the new number, i.e.:
             (new + k)% n = old.
             F (n) = (f (n-1) + k)%n
Then the above recursion is proven.
    Using this theorem, the result can be obtained in the time of O (n).


    The 2nd recursive method is an improvement of the first recursive method, so that the value of f (n) is no longer calculated directly from F (n-1), and the "jumping" of a certain distance is realized when the enumeration is calculated. Here's how:
    for n candidates, when all candidates are scanned once, all numbers modulo k results in k-1 candidates are eliminated. Namely: satisfies the number s% k = k-1, will be eliminated, at this time, altogether eliminated the n-n/k individual. Example:
at this point, similar to Method 1, the numbering is reset after the last retired person, as follows (assuming there are no 3k-1 after the figure):
    At this point, observe the relationship between the old and new numbers, you can find: the figure of the green part of the numbering relationship for the new number plus n-n%k modulo n is the old number:
Old = new + N-n%k
And the blue part of the front, the new and old numbering relationship is not so obvious, the main reason is that the blue part of the sequence is not continuous. As you can see, the deleted data divides the entire blue part into successive sequences of several lengths of K-1. So this part of the new and old number of the conversion relationship by the new number covered by how many k-1 sequence to decide, each covering a k-1 sequence, then the number will be in the original conversion results (Method 1 in the conversion mode) based on the 1-bit offset.
Therefore: the conversion formula for the blue part is as follows:
Old = (new + n-n%k)% n + (new-n% k)/(K-1)
= New-n%k + (new-n%k)/(K-1)
The above is the 2nd recursive calculation method, the method requires n must not be less than K.

So in the solution of the idea is: first determine whether n is greater than K, if n>k, then the use of the method (2) to convert f (n) to a smaller size problem F (n-n/k), the method (2) until n<k, and then adopt the method (1), the F (n) converted to F (n-1 ) until the solution.

The algorithm can be used to solve the time complexity of O (n).

input/output format:
      input: First enter a positive integer t to represent the number of groups of test data, and the next T-line each behaves 2 integers n,k. &NBSP;
     output:

data range:
      1≤t≤100
     2≤n≤1,000,000,000
      2≤k≤1,000 

Program code:

/****************************************************//* File:Hiho_Week_94.cpp *//* Author : Zhang Yufei *//* date:2016-04-19 *//* description:hihocoder A CM program. (submit:g++) *//****************************************************/#include <stdio.h>/* * This function deals with one test case. * Parameters: *none. * Returns: *none.  */void function (void);/* This function get the result of the given N and K. * Parameters: *@n:the number of candidate. *@k:the ' K ' parameter in this question. * Returns: *the result for the given N and K. */int compute (int n, int k);/* * The main program. */int Main (void) {int t;scanf ("%d", &t), for (int i = 0; i < T; i++) {function ();} return 0;} /* * This function is deals with the one test case. * Parameters: *none. * Returns: *none. */void function (void) {int n, k;scanf ("%d%d", &n, &k);p rintf ("%d\n", Compute (n, k));} /* * This function get the RESult of the given N and K. * Parameters: *@n:the number of candidate. *@k:the ' K ' parameter in this question.  * Returns: *the result for the given N and K. */int compute (int n, int k) {if (n = = 1) {return 0;} if (n < k) {return (k + Compute (n-1, k))% n;  } else {int r = Compute (N-n/k, k), if (R < n k) {return r-n% k + N;} else {return r-n% k + (r-n% k)/(K-  1);}}}


ACM: Special topic in number Theory (3)--Joseph's question

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.