#include <iostream>#include<cmath>using namespacestd;intMain () {intn,sum=0, j,i,k,lpl,a[100000],b[100000]; CIN>>N; a[1]=1, b[1]=1; for(intI=2; i<=n;i++) {A[i]= (a[i-1]+1)%i+1; if(a[i]==i) b[i]=i; Elseb[i]=B[a[i]]; } cout<<b[n]+N;}
Again a recursive code as above;
Question Description:
You must have heard of the classic "Joseph" question, right? Now let's organize a Happy new game: Assume that n individuals stand in a circle, from the 1th person to the alternate removal of the player, but only temporarily remove (for example, first remove 2), until the last remaining survivors. After the survivors have been elected, all those who are taller than the survivor number will each receive 1 dollars and leave permanently, and the remainder will repeat the process, leaving 1 dollars per person than the survivor number. Once this process has passed, the number of people is no longer reduced, and the last remaining ones will receive 2 dollars. Please calculate how much money the organizer will pay?
For example, the first round has 5 people, the survivors are 3, so 4, 5 get 1 dollars left, the next survivor is still 3, so no one left, so everyone get 2 yuan, a total of pay 2+2*3=8 dollars.
Input:
One line of an integer n.
Output:
A single integer, not more than 65535, indicates how much money will be paid in total.
Input instance:
10
Output instance:
13
First of all, everyone gets 1 dollars, and only the last survivors get 1 dollars more. So just ask for the last person who will survive.
Assuming that the final (m) individual is left after the M-ring operation, the number of people can no longer be reduced, then the solution to the problem should be final (m) +n. But how do you find final (m)?
When the final (i) =i of the first time, the number of people will not be reduced, at this time I is M, otherwise, it is necessary to the remaining final (i) individuals to carry out the ring operation;
There are two situations: 1 set Jose (i) for the survivor's number, the person who reported K out, then Jose (i-1) can be understood as the first round off, K out after the state, K go out after the k+1 continue to count, there are i-1 individuals in the circle, from the k+1 began to count, numbered Jose (i) For: k+1,k+2......i,1,2.....,k-1;
2 can artificially turn this circle counterclockwise to K units, at this time the number of the numbered Jose (i-1): 1,2.....,i-k,i-k+1,i-k+2....,i-1;
In both cases, all data except I and i-k are found to satisfy the law: Jose (i) = (Jose (i-1) +k) MoD I. A little tweak to the equation, the formula is fulfilled: jose (i) = (Jose (i-1) +1) mod i+1;
At this point the recursive formula comes out, the boundary condition is Jose (1) =1. Then, each Jose (i) is deduced, until a certain Jose (i) =i, then final (i) =i, otherwise final (i) =final (Jose (i)).
So that's it!!!
C + + New Testament Joseph question