Sha 305 Joseph

Source: Internet
Author: User

Idea: Mathematics + tabulation
Analysis:
1. The traditional Joseph question is given n people and m. The current person is kicked out every m times and the number of the person left behind
2. The first k are good people, and the last k are bad people. Now the smallest m is required so that k bad guys are kicked out when no good guy is kicked out.
3. According to the traditional method, the number of n persons ranges from 0 ~ N-1
The first time a [1] = (m-1) % n; // The number of the person is 0 ~ N-1
Second a [2] = (a [1] + m-1) % (n-1 );
I times a [I] = (a [I-1] + m-1) % (n-I + 1 );
So we can know the number of the person to be deleted each time. Since k is up to 14, we can first create a table to find 1 ~ 14, and then output.

Code:


 

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;int solve(int k){    int pre;    int tmp = k+1;    int n = 2*k;    while(tmp){        if((tmp-1)%n >= k && (tmp-1)%n < 2*k){            pre = (tmp-1)%n;            int i;            for(i = 2 ; i <= k ; i++){               int x = (pre+tmp-1)%(n-i+1);                if(x < k || x >= 2*k)                   break;               else                   pre = x;            }             if(i == k+1)                return tmp;        }        tmp++;    }}int main(){    int k , ans[20];    for(int i = 1 ; i < 15 ; i++)        ans[i] = solve(i);    while(scanf("%d" , &k) && k)        printf("%d\n" , ans[k]);    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.