Example 26 variants of the Joseph problem (and then there was One,japan 2007,la 3882)

Source: Internet
Author: User



Title Description

n the number of rows in a circle. Delete m for the first time, delete each k number once, and ask for the last number to be deleted.



Problem analysis

First, in order to take the mold, n personal number is 0,1,2......n-1, when only 1 people, it must be numbered 0 (only it), so dp[1]=0,

When there are two people, the last remaining (dp[1]+k)%2, is the reverse thinking process, so dp[i]= (dp[i-1]+k)%i;

There is also because the first time is the deletion of M, then the difference will be added to the margin, so ans= (dp[n]+k-m)%n; Due to the k,m difference

The value may be very small negative numbers, so that ans is negative, so when ans<0, Ans+=n, finally will ans+1, will be easy to take the mold

Minus 1 to make up.


Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace Std;const int maxn=100000+100;int Dp[maxn];int main () {    int n,m,k;    while (~SCANF ("%d%d%d", &n,&k,&m) &&n)    {        dp[1]=0;        for (int i=2;i<=n;i++)        dp[i]= (dp[i-1]+k)%i;        int ans= (dp[n]+ (m-k))%n;        if (ans<0)        ans+=n;        printf ("%d\n", ans+1);    }    return 0;}




Example 26 variants of the Joseph problem (and then there was One,japan 2007,la 3882)

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.