Zoj 1088 Solution--josephus problem, speed up resolution

Source: Internet
Author: User
Tags time 0

<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " >zoj 1088 title to the main idea is that the N building power outage, first stop first, and then separate m building stop a building. Count to the last one after the start cycle counting, has been powered off the count of not participating. To pick the right m, the second building still has electricity, even if the other buildings are out of power. </span>

If the problem is considered a normal simulation algorithm, its time complexity will be up to O (m*n). Because the topic gives ample time and a small m, n range, the general simulation algorithm can be completed. But there is no more efficient algorithm.

The common optimization method is that every time a building is disconnected, the problem size is reduced by one, so that the complexity is reduced to O (n). Below reprint content:

Problem description: N Person (number 0~ (n-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 n-1 individuals formed a new Joseph Ring (starting with the person numbered k=m%n):
K k+1 k+2 ... n-2, n-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:

After the transformation has completely become the (n-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 above table to turn this x back is not exactly the solution of n personal situation?! The formula to change back is very simple, I believe everyone can push out: X ' = (x+k)%n

How to Know (n-1) The solution of the problem of personal count off? Yes, as long as you Know (n-2) the individual's solution. (n-2) A personal solution? Of course, the first thing to ask (n-3)----This is obviously a backward problem! Okay, here's the idea, here's the recursive formula:

Make f[i] means I personally play the game reported M exit the last winner's number, the final result is naturally f[n]

Recursive formulas
f[i]= (f[i-1]+m)%i; (i>1)

In general, C + + time using this method consumes more than 20ms, which takes up approximately 272kB of memory.

You can also use the characteristics of J (2^n) =1 to optimize the calculation, that is, for the original problem, for the n=2^p situation, the direct output 1 to avoid the calculation. Last consumption time 0, Space 168kb (c + + consumption equivalent).

The following posted my original code. This algorithm is already very close to the best ranking 160KB. But do not know how to continue to reduce space consumption. Hope to be able to optimize after the completion of the COM book.

#include <cstdio>using namespace Std;//arthur::xiaoqiang anint Joseph (int k,int b) {int res=0;for (int i=2;i<=k; ++i) res= (res+b)%i;return Res;} int main () {int n,t;while (scanf ("%d", &n), N) {t=n-1;if (t==2| | t==4| | t==8| |t==16| | t==32| | t==64| | t==128) {printf ("2\n"); continue;} else{for (int m=3;; ++M) if (Joseph (t,m) ==0) {printf ("%d\n", m); break;}}}

If you simulate directly and then output all the results to an array, you can also reduce the time to 0, but the space consumption may be greater than 168KB. I use my own previous code, output the results to text, and re-check the table output code as follows:

(To make an ad for Vim, the process of transforming the output to an array value is all done using VIM, up to 10 operations, one minute can be done, it is highly recommended to use VIM programming).

#include <stdio.h>int main () {int m[153];m[0]=-1;m[1]=-1;m[2]=-1;    m[3]=2;    m[4]=5;    m[5]=2;    m[6]=4;    m[7]=3;    m[8]=11;    m[9]=2;    m[10]=3;    m[11]=8;    m[12]=16;    m[13]=4;    m[14]=21;    m[15]=6;    m[16]=5;    m[17]=2;    m[18]=11;    m[19]=20;    m[20]=34;    m[21]=8;    m[22]=15;    m[23]=10;    m[24]=7;    m[25]=13;    m[26]=11;    m[27]=13;    m[28]=45;    m[29]=18;    m[30]=23;    m[31]=8;    m[32]=3;    m[33]=2;    m[34]=25;    m[35]=75;    m[36]=42;    m[37]=13;    m[38]=5;    m[39]=23; m[40]=13;    m[41]=50;    m[42]=16;    m[43]=18;    m[44]=89;    m[45]=38;    m[46]=8;    m[47]=39;    m[48]=30;    m[49]=29;    m[50]=38;    m[51]=7;    m[52]=45;    m[53]=23;    m[54]=137;    m[55]=46;m[56]=63;    m[57]=17;    m[58]=48;    m[59]=5;    m[60]=46;    m[61]=34;    m[62]=140;    m[63]=33;    m[64]=39;    m[65]=2;    m[66]=28;    m[67]=29;    m[68]=79;    m[69]=33;    m[70]=48;    m[71]=3;    m[72]=10;    m[73]=46;    m[74]=120;    m[75]=6;    m[76]=37;m[77]=17;    m[78]=8;    m[79]=44;    m[80]=15;    m[81]=160;    m[82]=20;    m[83]=35;    m[84]=144; m[85]=104;    m[86]=179;    m[87]=153;    m[88]=24;    m[89]=8;    m[90]=265;    m[91]=19;    m[92]=9;    m[93]=62;    m[94]=7;    m[95]=139;    m[96]=19;    m[97]=44;    m[98]=93;    m[99]=182;    m[100]=27;    m[101]=158;    m[102]=185;    m[103]=193;    m[104]=17;    m[105]=82;    m[106]=3;    m[107]=11;    m[108]=43;    m[109]=55;    m[110]=21;    m[111]=41;    m[112]=146;    m[113]=29;    m[114]=80;    m[115]=59;    m[116]=8;    m[117]=29;    m[118]=66;    m[119]=19;    m[120]=160;    m[121]=59;    m[122]=28;    m[123]=129;    m[124]=127;    m[125]=120;    m[126]=72;    m[127]=45;    m[128]=157;    m[129]=2;    m[130]=63; m[131]=127;    m[132]=81;    m[133]=318;    m[134]=513;    m[135]=98;    m[136]=28;    m[137]=32;    m[138]=231;    m[139]=236;    m[140]=411;    m[141]=26;    m[142]=45;    m[143]=5;    m[144]=303;    m[145]=228;    m[146]=66;    m[147]=9;    m[148]=205; M[149]=65;    m[150]=39;    m[151]=16;    int n;    while (scanf ("%d", &n), N) {printf ("%d\n", M[n]); }return 0;}

Zoj 1088 Solution--josephus problem, speed up resolution

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.