Joseph Ring Question

Source: Internet
Author: User

Title Description:

Give n personal siege a ring, starting from the M number, every time k-1 individuals (from the last person killed in the next number K, the first K-man was killed), ask the last remaining person is the date.

BC 75 field has a side shape, uvalive3882 is also a deformation;

Ideas:

We can solve it by hand, setting F[n] indicates who is the nth person killed.

First we numbered people starting with 0 and then 0~n-1.

f[1] = 0;

The first time we killed the k-1, and then we re-numbered, starting with the original K, numbering number No. 0,

Then if we know who the remainder of the remaining n-1 is, then this person's number is added to the value of K;

And then push back a bit.

Well, we know that in the end, when there is a person, his number must be 0, and must die, so there is the above f[1] = 0;

So what was his last number?

If k = 3, then his number should be (0+3)%2 = 1;

Let's verify that 0, 1, 0, 0 are dead, 1 are left. That's right.

So.... It is very simple, the hand push is over, remember to add 1;

   

/************************************************************************* > File Name:uvalive3882.cpp > Author:baiyan > Test Instructions: > Created time:2016 May 28 Saturday 17:08 47 sec. *************************************************** *******************/#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;intf[10005];intMain () {intn,m,k;  while(SCANF ("%d%d%d", &n,&k,&m)!=eof && (n| | k| |m) {memset (f),0,sizeof(f)); f[1]=0;  for(intI=2; i<=n;i++)        {            if(i<N) f[i]= (f[i-1]+K)%i; ElseF[i]= (f[i-1]+M)%i; //This is the number of people who are currently killed.} printf ("%d\n", f[n]+1); }    return 0;} 

Joseph Ring Question

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.