当前位置:网站首页>Mathematical solution of Joseph Ring
Mathematical solution of Joseph Ring
2022-06-30 03:09:00 【The coolness of Si Ku Quan Shu】
List of articles
subject
【 Joseph ring 】
this n Number in a circle , From numbers 0 Start , Delete the... From this circle every time m A digital . Find the last number left in the circle .
for example ,0,1,2,3,4 this 5 Numbers make a circle , From numbers 0 Start deleting the 3 A digital , Before deleting 4 The numbers in turn are 2,0,4,1, So the last remaining number is 3.
Mathematical method derivation
Solve the Joseph Ring problem , We use backward push , Push us down : The last remaining number , Position in the first array .
The last number left ( abbreviation “ it ”) When , The total number is 1, Its subscript pos = 0.
So it was safe in the last round , The total number is 2, Its subscript (0+m)%2;
( explain : In the last round , The number in front of it ( That is, the red number , Subscript to be
m-1) It was removed ; So its subscript ism; Because it's a ring , Therefore need%2)
Then it is safe on the last wheel , The total number is 3, Its subscript ((0+m)%2+m)%3;
Then it is safe to go on the upper wheel , The total number is 4, Its subscript (((0+m)%2+m)%3+m)%4;
…
So it is safe in the first round of the game , The total number is n, Its subscript pos That's what I'm looking for .
That is, if you push back from the bottom up : If the subscript of its next round is pos, Then the subscript of the current round is :(pos+m)% Number of people in the current round .
Last , Because the number given is nums = 0,1,2,…,n-1, namely nums[i] = i, So find the subscript [ The formula ] It's equivalent to finding this number .
Solve the code of Joseph Ring
#include<iostream>
#include<list>
using namespace std;
int main(){
int m,n;
cin >> n >> m;
int idx = 0;
for(int i = 2; i <= n; ++i){
idx = (idx + m)% i;
}
cout << idx + 1 << endl;
return 0;
}
Advanced application of this mathematical method
【 Counting game 】100 A circle of individuals , Everyone has a code , Number from 1 Start to 100. They come from 1 Start counting in turn , Check in as M People automatically quit the circle , Then the next person goes from 1 Start counting , Until the remaining number is less than M. What is the original number of the last remaining people ?
#include<iostream>
#include<vector>
using namespace std;
int main(){
int m;
cin >> m;
vector<int> cap(m - 1,0);
for(int i = 0; i < m - 1;++i){
cap[i] = i;
}
for(int i = m; i <= 100; ++i){
for(int j = 0; j < m - 1; ++j){
cap[j] = (cap[j] + m) % i;
}
}
for(int i = 0; i < m - 1; ++i){
cout << cap[i] + 1 << endl;
}
return 0;
}
边栏推荐
- Use of Arthas
- Auto. JS learning notes 16: save to the mobile phone by project, instead of saving a single JS file every time, which is convenient for debugging and packaging
- Sorting method of administrative route code letter + number
- JS 字母和数字的相互转换
- Jvxetable sub table record loading completion event
- Formal and actual parameters, value passing and address passing
- What kind of foreign exchange trading platform is regulated and safe?
- [live broadcast notes 0629] Concurrent Programming II: lock
- 2022 new test questions for safety management personnel of metal and nonmetal mines (small open pit quarries) and certificate examination for safety management personnel of metal and nonmetal mines (s
- Principle, advantages and disadvantages of three operating modes of dc/dc converter under light load
猜你喜欢

怎么使用Vant实现数据分页和下拉加载

Federal learning: dividing non IID samples by Dirichlet distribution

mysql 主从数据库同步失败的原因

Simple custom MVC

Distributed file storage system fastdfs hands on how to do it

Prompt learning a blood case caused by a space

Auto. JS learning notes 16: save to the mobile phone by project, instead of saving a single JS file every time, which is convenient for debugging and packaging

Gulang bilibilibili Live Screen Jackie

What is the concept of string in PHP

HTA introductory basic tutorial | GUI interface of vbs script HTA concise tutorial, with complete course and interface beautification
随机推荐
WPF Initialized事件在.cs中绑定不被触发的原因
Servlet interview questions
Redis+AOP怎么自定义注解实现限流
High paid programmers & interview questions series 63: talk about the differences between sleep (), yield (), join (), and wait ()
Gulang bilibilibili Live Screen Jackie
问题记录:fel_lib.c:26:10: fatal error: libusb.h: 没有那个文件或目录
PHP two-dimensional array randomly fetches a random or fixed number of one-dimensional arrays
简单自定义MVC优化
华为面试题: 高矮个子排队
Raki's notes on reading paper: Leveraging type descriptions for zero shot named entity recognition and classification
可视化HTA窗体设计器-HtaMaker 界面介绍及使用方法,下载 | HTA VBS可视化脚本编写
How to modify and add fields when MySQL table data is large
Compile a DLL without import table
MySQL extracts strings from table fields
How to use redis to realize the like function
C console format code
Intel-Hex , Motorola S-Record 格式详细解析
What are outer chain and inner chain?
Jvxetable增加自定义按钮
个人PC安装软件