当前位置:网站首页>Detailed explanation of Joseph problem
Detailed explanation of Joseph problem
2022-06-13 01:09:00 【dddd_ jj】
1. What is Joseph's problem
It has a number of 1,2,……,n Of n(n>0) A circle of individuals , From 1 Individual starts counting , To report for duty m Stop reporting when , newspaper m People out of circles , And then from his next person to report , To report for duty m Stop reporting when , newspaper m Out of the loop ,……, Go on like this , Until everyone is out of the circle . When any given n and m after , Design algorithm n The order in which individuals go out of circles .
2. Violence solution
The most direct method is to simulate the process of counting . simulation n-1 The process of counting off the number of times , Report data each time m personal , The first m Individuals are eliminated . Finally, check which number did not check in , This number is the result .# Learning goals :
But the time complexity is O(m*n). So when m and n When you're older , The running time of the program will be very long .
3. Recursive formula solution
Step one : Definition f(n)
take n Personal count , newspaper m The problem of people out of the circle is regarded as f(n,m) problem , And let the solution of the problem be f(n). that n-1 Personal count , newspaper m The problem of people out of the circle is regarded as f(n-1,m) problem , And let the solution of the problem be f(n-1).
| f(n,m) | n Personal count , newspaper m People out of circles |
|---|---|
| f(n-1,m) | n-1 Personal count , newspaper m People out of circles |
| … | … |
| f(1,m) | 1 Personal count , newspaper m People out of circles |
Step two find f(n) And f(n-1) The relationship between
Assume that at the beginning, everyone's serial number is :
1 2 3 … n-1 n
So after the first count off , Only the following people are left :
1 2 3 … m-1 m+1 … n-1 n
And after the next count off , from m+1 Start counting , So the serial number should change to :
m+1 m+2 …n-1 n 1 2 3 … m-1
( Let the serial number here be x)
Map the above serial number to the following serial number :
1 2 3 … n-2 n-1
( Let the serial number here be y)
be x and y What's the relationship :
x=(y+m)%n
among , The serial number here x It stands for f(n,m) The serial number in , Serial number y It stands for f(n-1,m) The serial number in . that f(n) And f(n-1) What's the relationship
f(n) =( f(n-1)+m )%n
f(n-1)=( f(n-2)+m )%(n-1)
f(n-2)=( f(n-3)+m )%(n-2)
…
f(2)=( f(1)+m )%2
f(1)=1
f(1)=1 Because there is only one person , It must be the last one left , Serial number is 1.
Step three Write code according to recurrence formula
According to the recurrence formula ,f(1)=1 after , Then you can get the answer from the bottom up . Get it first f(2). Ask again f(3) until f(n). So here's the code .
int yuesefu(int n, int m) {
int x = 1;
for (int i = 2; i <= n; i++) {
x = (x + m) % i;
}
return x;
}
边栏推荐
- Argparse command line passes list type parameter
- [JS component library] drag sorting component
- Unity calls alertdialog
- Stmarl: a spatio temporal multi agentreinforcement learning approach for cooperative traffic
- Continue when the condition is not asked, execute the parameter you compare
- How to solve the problem of database?
- Downloading wiki corpus and aligning with multilingual wikis
- Jenkins continuous integration operation
- What is dummy change?
- sort
猜你喜欢

Stmarl: a spatio temporal multi agentreinforcement learning approach for cooperative traffic

Unity calls alertdialog

Five classic articles worth reading

How to choose stocks? Which indicator strategy is reliable? Quantitative analysis and comparison of DBCD, ROC, vroc, Cr and psy index strategy income
![[003] embedded learning: creating project templates - using stm32cubemx](/img/18/43dfa98f1711e8e544828453e36812.jpg)
[003] embedded learning: creating project templates - using stm32cubemx

五篇经典好文,值得一看(2)

Alexnet实现Caltech101数据集图像分类(pytorch实现)
![[JS component library] drag sorting component](/img/f9/4090b52da1a5784b834cb7dbbb948c.jpg)
[JS component library] drag sorting component

Binary tree -- using hierarchical sequence and middle sequence to determine a tree

Introduction to convolutional neural network
随机推荐
Binary tree traversal - recursive and iterative templates
Wikipedia User Guide
Three threads print digital demo alternately
Binary tree -- using hierarchical sequence and middle sequence to determine a tree
Status of the thread
Deadlock problem summary
Argparse command line passes list type parameter
4K sea bottom and water surface fabrication method and ocean bump displacement texture Download
Rasa对话机器人之HelpDesk (三)
Illustrator tutorial, how to add dashes and arrows in illustrator?
Quantitative investment traditional index investment decision vs Monte Carlo simulation method
Wangdao machine test - Chapter 6 - maximum common divisor GCD and minimum common multiple LCM
Expression tree - medium order printout
Introduction to convolutional neural network
Install pycharm process
How many rounds of deep learning training? How many iterations?
论文笔记:STMARL: A Spatio-Temporal Multi-AgentReinforcement Learning Approach for Cooperative Traffic
ArrayList underlying source code
Androi weather
Mathematical knowledge arrangement: extremum & maximum, stagnation point, Lagrange multiplier