当前位置:网站首页>约瑟夫环 数学解法
约瑟夫环 数学解法
2022-06-30 02:55:00 【四库全书的酷】
题目
【约瑟夫环】
这 n 个数字排成一个圆圈,从数字 0 开始,每次从这个圆圈里删除第 m 个数字。求出这个圆圈里剩下的最后一个数字。
例如,0,1,2,3,4 这 5 个数字组成一个圆圈,从数字 0 开始每次删除第 3 个数字,则删除的前 4 个数字依次是2,0,4,1,因此最后剩下的数字是 3。
数学方法推导
解决约瑟夫环问题,我们采用倒推,我们倒推出:最后剩下的这个数字,在最开始的数组中的位置。
剩下最后一个数字(简称“它”)的时候,总个数为 1,它的下标pos = 0。
那么它在上一轮也是安全的,总个数为 2,它的下标 (0+m)%2;
(解释:在上一轮中,它前面的数字(即红色的数字,下标为
m-1)被移走了;因此它的下标是m;由于是环,因此需要%2)
那么它在上上轮也是安全的,总个数为3,它的下标 ((0+m)%2+m)%3;
那么它在上上上轮也是安全的,总个数为4,它的下标 (((0+m)%2+m)%3+m)%4;
…
那么它在游戏开始的第一轮也是安全的,总个数为n,它的下标 pos就是所求。
即如果从下向上反推的时候:假如它下一轮的下标为pos,那么当前轮次的下标就是:(pos+m)%当前轮次的人数。
最后,由于给出的数字是nums = 0,1,2,…,n-1,即nums[i] = i,因此找出下标 [公式] 就相当于找到这个数字。
解决约瑟夫环的代码
#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;
}
该数学方法的进阶应用
【报数游戏】100个人围成一圈,每个人有一个编码,编号从1开始到100。他们从1开始依次报数,报到为M的人自动退出圈圈,然后下一个人接着从1开始报数, 直到剩余的人数小于M。请问最后剩余的人在原先的编号为多少?
#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;
}
边栏推荐
- CMake教程系列-03-依赖管理
- How to switch ipykernel to a different CONDA virtual environment in jupyterlab?
- 怎么利用Redis实现点赞功能
- 中断操作:AbortController学习笔记
- 2022 the action of protecting the net is imminent. Things about protecting the net
- How can redis+aop customize annotations to achieve flow restriction
- 怎么使用Vant实现数据分页和下拉加载
- Wechat applet +php to realize authorized login operation
- Redis+AOP怎么自定义注解实现限流
- Multi card server usage
猜你喜欢

Summary of PHP test sites encountered in CTF questions (I)

Time complexity analysis

Cross domain, CORS, jsonp

How to use vant to realize data paging and drop-down loading

Raki's notes on reading paper: Leveraging type descriptions for zero shot named entity recognition and classification

自定义JvxeTable的按钮及备注下$set的用法

IBM WebSphere channel connectivity setup and testing

CMake教程系列-02-使用cmake代码生成二进制

Software testing skills, JMeter stress testing tutorial, transaction controller of logic controller (25)

Mysql提取表字段中的字符串
随机推荐
2.8 【 weight of complete binary tree 】
Simulate activity startup mode in compose
Mysql提取表字段中的字符串
Use compose to realize the effect of selecting movie seats by panning tickets
JMeter obtains cookies across thread groups or JMeter thread groups share cookies
How to modify and add fields when MySQL table data is large
Idea remote debugging remote JVM debug
FAQs for code signature and driver signature
原生JS怎么生成九宫格
LeetCode 3. 无重复字符的最长子串
Xunwei NXP itop-imx6 development platform
Unity3D UGUI强制刷新Layout(布局)组件
Linear algebra Chapter 4 Summary of knowledge points of linear equations (Jeff's self perception)
JvxeTable子表记录加载完毕事件
How to prevent duplicate submission under concurrent requests
Enlightenment from the revocation of Russian digital certificate by mainstream CA: upgrade the SSL certificate of state secret algorithm to help China's network security to be autonomous and controlla
What kind of foreign exchange trading platform is regulated and safe?
CMake教程系列-01-最小配置示例
How does native JS generate Jiugong lattice
How to use vant to realize data paging and drop-down loading