POJ 1781 in Danger

Source: Internet
Author: User
Tags cmath

Title Link: http://poj.org/problem?id=1781


In Danger
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 3623 Accepted: 1889

Description

Flavius Josephus and fellow rebels were trapped by the Romans. His companions preferred suicide to surrender, so they decided to form a circle and to kill every third person and to proc Eed around the circle until no one is left. Josephus was wasn't excited by the "the idea of killing himself" so he calculated the position "The Last Man Standing (and th En he did not commit suicide since nobody could watch).

We'll consider a variant of this ' game ' where every second person leaves. And of course there would be is more than persons, for we now has computers. You had to calculate the safe position. Be careful because we might apply your program to calculate the winner of this contest!

Input

The input contains several test cases. Each specifies a number n and denoting the number of persons participating in the game. To make things more difficult, it always have the format "Xyez" with the following Semantics:when N was written down in Dec iMAL notation, its first digit are X, its second digit are Y, and then follow Z zeros. Whereas 0<=x,y<=9, the number of zeros is 0<=z<=6. Assume that n>0. The last test was followed by the string 00e0.

Output

For each test case generate a line containing the position of the person who survives. Assume that the participants has serial numbers from 1 to N and so the counting starts with person 1, i.e., the first P Erson leaving is the one with number 2. For example, if there be 5 persons in the circle, counting proceeds as 2, 4, 1, 5 and person 3 is staying alive.

Sample Input

05e001e142e066e600e0

Sample Output

352164891137

Source

ULM Local 2004
problem: Joseph question ^_^the problem of n is calculated according to Xyez, such as xyez for 05e0 n=5* 10^0 =5 xyez for 01e1 n= 1* 10^1 = ten the meaning of the topic and POJ3517 (point I see) almost , equivalent to POJ3517 in the M and K are 2 ~_~ that is not the POJ3517 You can change it a little bit. Haha, it's not so simple. The previous question I was written in a recursive, put here, the words will explode stack, becauseThe data for this problem is relatively large, do not believe that you can try 66e6. The question is quite interesting. I started on the draft paper. N equals 1 2 3 ... the situation is calculated, and found that there seems to be a regular. So I just used thatRecursive code to verify (recursion is still possible when the data is relatively small)

Here's what I counted.

when n is 2^x, the corresponding output is 1 2^x-1 the corresponding value is 2^x-1
for a number n when 2^x < = n <2^x-1 the output of n corresponds to (n-2^x) *2+1 on paper can be pushed out ^_^
AC Code:
#include <iostream> #include <cmath>using namespace Std;int x,y,z,n;char ch;int main () {while    (cin> >X>>CH>>Z) {        if (!x&&!z) break;        N=x*pow (10.0,z);        int Pos=log ((double) n)/log (2.0);        N-=pow (2.0,pos);        n=n*2+1;        cout<<n<<endl;    }    return 0;}

Attach a recursive code for the stack
#include <iostream> #include <cmath>using namespace Std;int x,y,z,n;char ch;int Ring (int cnt,int num) {    int temp;    if (cnt==n) {        temp= (num+2)%cnt;        if (temp) return temp;        else return cnt;    }    temp= (num+2)%cnt;    if (!temp) temp=cnt;    Return Ring (cnt+1,temp); int main () {    while (cin>>x>>ch>>z) {         if (!x&&!z) is break;        N=x*pow (10.0,z);        Cout<<ring (2,1) <<endl;    }}






POJ 1781 in Danger

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.