Lightoj 1179 Joseph Problem

Source: Internet
Author: User

1179-Joseph Problem
PDF (English) Statistics Forum
Time Limit: 2 second (s) memory limit: 32 MB
The historian Flavius Joseph PHUs relates how, in the Romano-Jewish conflict of 67. D ., the Romans took the town of jotapata which he was commanding. escaping, Joseph found himself trapped in a cave with 40 companions. the Romans discovered his whereabouts and invited him to surrender, but his companions refused to allow him to do so. he therefore suggested that they kill each other, one by one, th E order to be decided by lot. tradition has it that means for affecting the lot was to stand in a circle, and, beginning at some point, Count round, every third person being killed in turn. the sole implements vor of this process was Joseph us, who then surrendered to the Romans. which begs the question: Had Joseph PHUs previusly practiced quietly with 41 stones in a dark corner, or had he calculated m Athematically that he shoshould adopt the 31st position in order to keep ve?

Now you are in a similar situation. there are n persons standing in a circle. the persons are numbered from 1 to n circularly. for example, 1 and N are adjacent and 1 and 2 are also. the count starts from the first person. each time you count up to K and the kth person is killed and removed from the circle. then the count starts from the next person. finally one person remains. given N and K you have to find the position of the last person who remains alive.

Input
Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case contains two positive integers n (1 ≤ n ≤ 105) and K (1 ≤ k <231 ).

Output
For each case, print the case number and the position of the last remaining person.

Sample Input
Output for sample input
6
2 1
2 2
3 1
3 2
3 3
4 6
Case 1: 2
Case 2: 1
Case 3: 3
Case 4: 3
Case 5: 2
Case 6: 3


Problem setter: Jane Alam Jan

/*************************************    author   : Grant Yuan    time     : 2014/8/21 13:35    algorithm: Josephus Problem    source   : LightOj 1179*************************************/#include<bits/stdc++.h>using namespace std;int n,k,t,ans;int s;int main(){    scanf("%d",&t);    for(int i=1;i<=t;i++)    {         scanf("%d%d",&n,&k);         s=0;         for(int j=2;j<=n;j++)         {             s=(s+k)%j;         }       printf("Case %d: %d\n",i,s+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.