Bnuoj Musical Chairs Joseph ring non-recursive

Source: Internet
Author: User

/* Problem Description: N Person (number 0~ (N1-1)), starting from 0 count, reporting (M-1) exit, the remainder continues to count off from 0. The winner's number.


We know that the first person (the number must be m%n-1) after the dequeue, the rest of the n1-1 individuals formed a new Joseph Ring (starting with the person numbered k=m%n1):
K k+1 k+2 ... n1-2, n1-1, 0, 1, 2, ... k-2
And at the beginning of the K is reported 0.


Now let's do a conversion of their numbers:
n1=n+1;


K--0-->0
K+1-1-->1
K+2-2-->2
...
...
K-2-N1-2-->n-1
K-1-N1-1-->n




(3)
(1) (2) +k
K--0--and K
K+1-1-->k+1
K+2-2-->k+2
...                                                     ...
...                                                     ...
N1-2-> (N1-2-k)--(N1-2-K) +k
N1-1-> (N1-1-k)--(N1-1-K) +k
0---(N1-1-K) + (1)--(N1-1-K) + (1) +k
1---(N1-1-K) + (2)--(N1-1-K) + (2) +k
...
...
K-2---n1-1-k+ (K-1) (based on the corresponding relationship above his value is n1-2=n-1)--(N1-1-k) + (k-1) +k
K-1---n1-1-k+ (k) (based on the corresponding relationship above his value is N1-1=n)--(N1-1-k) + (k) +k
N1=n+1 into (3) after (1) ≡ (3) (mod n1) that is right plus K and left is about N1 is the same remainder
K--0
K+1-1
K+2-2
...
...
K-2-N-1
After the transformation has become completely (n1-1) personal count of the sub-problem, if we know the solution of this sub-problem: for example, X is the final winner, then
According to the table above, the x is changed back to not just the solution of the n personal situation?!! The formula that changes back is very simple, I believe everyone can push out:
X ' = (x+k)%n1,k=m%n1, because K is the remainder, so smaller than n1, x ' = (x%n1+k)%n1= (x%n1+m%n1)%n1= (x+m)%n i.e. x ' = (x+m)%n1, that is, the solution of the current n number is X,
N1=n+1,m is fixed, can be obtained through the formula, how to Know (n1-1) the problem of personal count off? Yes, as long as you Know (n1-2) the individual's solution. (n1-2) A personal solution? Of course it is.
First Ask (n1-3) The situation----This is obviously a backward problem! Well, the idea came out, the following to write a recursive formula: make F[i] means I play games
The number of the final winner of the report M exit, the final result is naturally f[n]
Recursive formulas
f[1]=0;
f[i]= (f[i-1]+m)%i; (i>1)

*/

#include <stdio.h>
int main ()
{
int n,m,i,s;
while (scanf ("%d%d", &n,&m)!=eof&& (n+m)!=0)
{
s=0;
for (i=2;i<=n;i++)
S= (s+m)%i;
printf ("%d%d%d\n", n,m,s+1);
}
return 0;
}

Bnuoj Musical Chairs Joseph ring non-recursive

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.