Poj 1012 & HDU 1443 Joseph (Joseph ring deformation)

Source: Internet
Author: User

Question link:

Poj 1012: http://poj.org/problem? Id = 1012

HDU 1443: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1443

Joseph ring (Baidu encyclopedia): http://baike.baidu.com/view/717633.htm? Fr = Aladdin


Description

The Joseph's problem is notoriously known. for those who are not familiar with the original problem: From among N people, numbered 1, 2 ,..., n, standing in circle every MTH is going to be executed and only the life of the last remaining person will be saved. joseph was smart enough to choose the position of the last remaining person, thus saving his life to give us the message about the incident. for example when n = 6 and M = 5 then the people will be executed in the order 5, 4, 6, 2, 3 and 1 will be saved.

Suppose that there are K good guys and K bad guys. in the circle the first K are good guys and the last K bad guys. you have to determine such minimal m that all the bad guys will be executed before the first good guy.

Input

The input file consists of separate lines containing K. The last line in the input file contains 0. You can suppose that 0 <k <14.

Output

The output file will consist of separate lines containing M corresponding to K in the input file.

Sample Input

340

Sample output

530

Source

Central Europe 1995

Question:

A total of K bad guys k good guys sit in a circle (the total number is 2 * K), the Front K is good guys (Number 1 ~ K), followed by K bad guys (number k + 1 ~ 2 K ),

The current report number is M. The report number starts from 1, and the person reporting to M will automatically die.

Why can m make K bad guys die before a good person dies?


Note: after the current round of M's personal death, the person numbered 1 in the next round is the person numbered m + 1 in the previous round,

If the previous round happens to be the last person who died, the next round will return to the start person and report "1"


K is relatively small, and the table is directly violent!


The Code is as follows:

# Include <cstdio> int main () {int P [17], ANS [17] = {0}; int M, K; For (k = 1; k <= 14; k ++) {int n = 2 * k; // total number of M = 1; for (INT I = 1; I <= K; I ++) {// Since the number is from 1 ans [I] = (ANS [I-1] + m-1) % (n-I + 1 ); if (ANS [I] <k) // it indicates that a good person is killed and does not match the meaning {I = 0; m ++; // enumeration} p [k] = m; // printf ("% d \ n", P [k]);} // return 0; int KK; while (scanf ("% d", & KK) & KK) {printf ("% d \ n", P [Kk]);} return 0 ;}


Poj 1012 & HDU 1443 Joseph (Joseph ring deformation)

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.