UVa 1394 The deformation of the Joseph problem

Source: Internet
Author: User

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem= 4140

Originally was to write this study code yesterday, and then last night bedroom and power off, can not help to spit groove here, well, the bedroom every day power off.

Test instructions is the input n,k,m three number, n the number of rows into a circle, the first delete m, after each number of K number deleted once, the last one to be deleted.

To the point, previously wrote a list of Joseph problem, but here must be timed out. Later read some reference, finally understand how to do.

The number of n from the 0 array, the first time after the deletion of the number of M, that is, the array subscript m-1 is deleted, at this time, the array is labeled as the number of M re as position No. 0, re-form a ring, then the subscript labeled M of the number of the subscript changed to 0.

The next step is the inverse derivation process, x ' = (x+m)%n.

Using an array to record where the winner is, a[1] means the place where the winner is, A[2] is the position of the winner where there are two people left, from above, a[1]=0.

Each subsequent deletion is to repeat the last process, the final left of the person must be in the position No. 0, then you can always push up to find out where the winner begins.

1#include <iostream>2#include <cstring>3 using namespacestd;4 5 Const intMAXN =10005;6 intA[MAXN];7 8 intMain ()9 {Ten     intN, K, m; One      while(Cin >> n >> k >> m && n && k &&m) A     { -Memset (A,0,sizeof(a)); -          for(inti =2; I < n; i++) theA[i] = (A[i-1] + k)%i; -         intAns = (A[n-1] + m)%N; -cout << ans+1<<Endl; -     } +     return 0; -}

UVa 1394 The deformation of the Joseph problem

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.