Roman Roulette (Joseph Ring Simulation)

Source: Internet
Author: User

Roman Roulette

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 286 Accepted Submission (s): 105


Problem DescriptionThe Historian Flavius Josephus relates how, in the Romano-jewish conflict of A.D., the Romans took T He town of Jotapata which he was commanding. Escaping, Jospehus found himself trapped in a cave with + companions. The Romans discovered his whereabouts and invited him to surrender, but he companions refused to allow him to does so. He therefore suggested that they kill all other, one by one, and the order to is decided by lot. Tradition has it, the means for effecting the lot is to stand in a circle, and, beginning at some point, count round, Every third person being killed in turn. The sole survivor of this process is Josephus, who then surrendered to the Romans. Which begs the Question:had Josephus previously practised quietly with + stones in a dark corner, or had he calculated M Athematically that he should adopt the 31st position in order to survive?

Have read an account of this gruesome event become obsessed with the fear so you'll find yourself in a similar s Ituation at some time on the future. In order to prepare yourself for such an eventuality you decide to write a program to run on your hand-held PC which would Determine the position that the counting process should start in order to ensure so you'll be the sole survivor.

In particular, your program should is able to handle the following variation of the processes described by Josephus. n > 0 People is initially arranged in a circle, facing inwards, and numbered from 1 to N. The numbering from 1 to n proceeds consecutively in a clockwise direction. Your allocated number is 1. Starting with person number I, counting starts in a clockwise direction, until we get to person number K (k > 0), who I s promptly killed. We then proceed to count a further k people in a clockwise direction, starting with the person immediately to the left of The victim. The person number k so selected have the job of burying the victim, and then returning to the position in the circle that T He victim had previously occupied. Counting then proceeeds from the person to he immediate left, with the kth person being killed, and so on, until only one Person remains.

For example, when n = 5, and k = 2, and i = 1, the Order of execution is 2, 5, 3, and 1. The Survivor is 4.

Inputyour program must read input lines containing values for N and K (in that order), and for each input line output the Number of the person with which the counting should begin in order to ensure that is the sole survivor. For example, the "in the above case" the safe starting position is 3. Input would be terminated by a line containing values of 0 for N and K.

Your program may assume a maximum of the people taking part in the this event.

Sample INPUT1 11 50 0

Sample OUTPUT1 1:

Flavy Joseph, a historian, has told the story of Rome's attack on his city in the Roman Jewish War of 67 BC. He then fled into a cave with 40 other fighters and was trapped inside. The Romans found his whereabouts and persuaded him to surrender, but his soldiers refused to surrender. So he proposed to kill each other, one by one, in order to be decided by everyone. There is a traditional way, in order to be fair, everyone is standing in a ring, starting at a certain point, the cycle count, every two living people killed one. Finally Joseph survived and surrendered to Rome. The question is, how does Joseph know that the 31st place in the beginning will survive until the end? is not taking advantage of the cave dark people did not notice, quietly with 41 stones to rehearse it? Or did he figure out the position in a mathematical way?

After reading this story carefully, you gradually overcome your fear and fear that it will happen to you in the future. But in response to this similar situation, you're going to write a program for your phone so you can quickly calculate the process and make sure you get to the last surviving position.

Similar to the question that Joseph said, your program needs to deal with the following adapted questions. Initially n (n > 0) Individuals face inward into a ring, and then clockwise numbered 1 to N. Your number is 1, the first one is numbered I, and the same clockwise direction starts counting, until K > 0 individuals kill him. And then from the next person who is still alive, count back to the K-man, who will bury the man who had just been killed and stand in the place of the slain man. And then from his left, the first person who is still alive begins to count, kill the K-man, and so on, until only one is alive.

For example, n = 5,k = 2,i = 1, the order of being killed should be: 2, 5, 3, and 1. 4 survived.

Analysis

Similar to the classical Joseph ring problem, this kind of problem can be derived from mathematical derivation to find the general formula. But the process of the problem is far more complex than the Joseph Ring, the analysis of the general formula is difficult, and because of the small amount of data (less than 100 people), can be directly according to the problem of the simulation type to solve.

The implementation of the simulation using dynamic arrays is very convenient and the process is simple. The array initially stores the number of each person, starting with the No. 0 element (number 1th), and not deleting the person's number before killing a person, but first identifying the person to bury him, swapping their numbers, and then deleting the place where the person who buried him was originally located. Finally calculated from the beginning of the Count 1th, the last can survive the number p, then from the first person in front of you you are safe (you stand in the 1th place), that is n–p. The principle of this conversion is obvious.

This is to find you in front of the first person to start you are safe;

This man will bury the man who had just been killed and stand in the place of the slain man.

Exercises

1#include <iostream>2#include <algorithm>3#include <cstdio>4#include <cstring>5#include <cmath>6#include <vector>7#include <map>8#include <stack>9#include <queue>Ten #defineMem (x, y) memset (x,y,sizeof (x)) One /*#define L TREE[ROOT].L A #define R TREE[ROOT].R - #define S Tree[root].sum - #define NOW Tree[root<<1].sum+tree[root<<1|1].sum the #define Lson Root<<1,l,mid - #define Rson Root<<1|1,mid+1,r*/ - using namespacestd; - Const intinf=0x3f3f3f3f; +vector<int>Vec; - intMain () { +     intn,k,flot=0; A      while(SCANF ("%d%d", &n,&k), n|k) { at vec.clear (); -          for(intI=1; i<=n;i++) - Vec.push_back (i); -             intpos=0, kisp= (K-1)%vec.size (); -          while(Vec.size () >1){ -Pos= (kisp-1+k)% (Vec.size ()-1); inPos= (pos+ (pos>=kisp))%vec.size (); -vec[kisp]=Vec[pos]; toVec.erase (Vec.begin () +POS); +Kisp= (kisp+k-(pos<kisp))%vec.size (); -         } theprintf"%d\n", (N-vec.front () +1)% n +1); *         //printf ("Case%d:%d\n", ++flot,*vec.begin ()); $     }Panax Notoginseng     return 0; -}

Roman Roulette (Joseph Ring Simulation)

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.