F - System Overload(约瑟夫环变形)
Description
Recently you must have experienced that when too many people use the BBS simultaneously, the net becomes very, very slow.
To put an end to this problem, the Sysop has developed a contingency scheme for times of peak load to cut off net access for some buildings of the university in a systematic, totally fair manner. Our university buildings were enumerated randomly from 1 to n. XWB is number 1, CaoGuangBiao (CGB) Building is number 2, and so on in a purely random order.
Then a number m would be picked at random, and BBS access would first be cut off in building 1 (clearly the fairest starting point) and then in every mth building after that, wrapping around to 1 after n, and ignoring buildings already cut off. For example, if n=17 and m=5, net access would be cut off to the buildings in the order [1,6,11,16,5,12,2,9,17,10,4,15,14,3,8,13,7]. The problem is that it is clearly fairest to cut off CGB Building last (after all, this is where the best programmers come from), so for a given n, the random number m needs to be carefully chosen so that building 2 is the last building selected.
Your job is to write a program that will read in a number of buildings n and then determine the smallest integer m that will ensure that our CGB Building can surf the net while the rest of the university is cut off.
Input Specification
The input file will contain one or more lines, each line containing one integer nwith 3 <= n < 150, representing the number of buildings in the university.
Input is terminated by a value of zero (0) for n.
Output Specification
For each line of the input, print one line containing the integer m fulfilling the requirement specified above.
Sample Input
3
4
5
6
7
8
9
10
11
12
0
Sample Output
2
5
2
4
3
11
2
3
8
16
解题思路:一开始第1层楼就已被断电,要求选出步长k使得每累加k层楼循环进行断电,要求最后剩下的一层楼是第2层楼。回顾一下约瑟夫环问题:已知n个人(0~n-1)围坐在一张圆桌周围。从编号0的人开始报数,数到k-1的那个人出列;他的下一个人又从0开始报数,数到k-1的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。而此题中第1个人(编号为0)就已经出列了,我们按递推式f[n]=(f[n-1]+k)%n计算出第n个人即最后一个人出列的编号f[n],这个一开始是第k-1个人出列,所以我们只需将原来的0~n-1重新编号,偏移量是1-k,标记第0个人在第k-1个位置(表示第一次就出列),则最终出列的编号为((f[n]+1-k)%n+n)%n(防止编号出现负数或者是超过n)。
AC代码:
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
int n,s;
while(cin>>n&&n){
for(int k=;;++k){//如果是1的话,第2层楼一开始就会被断电,因此步长从2开始
s=;
for(int j=;j<=n;++j)s=(s+k)%j;
s=((-k+s)%n+n)%n;
if(s==){cout<<k<<endl;break;}//第二层楼的编号为1,最后一个断网的楼层是2,即找到最小的步长k,直接退出
}
}
return ;
}
F - System Overload(约瑟夫环变形)的更多相关文章
- 【约瑟夫环变形】UVa 1394 - And Then There Was One
首先看到这题脑子里立刻跳出链表..后来继续看如家的分析说,链表法时间复杂度为O(n*k),肯定会TLE,自己才意识到果然自个儿又头脑简单了 T^T. 看如家的分析没怎么看懂,后来发现这篇自己理解起来更 ...
- HDU 5643 King's Game | 约瑟夫环变形
经典约瑟夫环 }; ; i<=n; i++) { f[i] = (f[i-] + k) % i; } 变形:k是变化的 #include <iostream> #include &l ...
- Poj 3517 And Then There Was One(约瑟夫环变形)
简单说一下约瑟夫环:约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数,数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个 ...
- poj 1012 & hdu 1443 Joseph(约瑟夫环变形)
题目链接: POJ 1012: id=1012">http://poj.org/problem?id=1012 HDU 1443: pid=1443">http:// ...
- G - And Then There Was One (约瑟夫环变形)
Description Let’s play a stone removing game. Initially, n stones are arranged on a circle and numbe ...
- C++版 - 剑指Offer 面试题45:圆圈中最后剩下的数字(约瑟夫环问题,ZOJ 1088:System Overload类似)题解
剑指Offer 面试题45:圆圈中最后剩下的数字(约瑟夫环问题) 原书题目:0, 1, - , n-1 这n个数字排成一个圈圈,从数字0开始每次从圆圏里删除第m个数字.求出这个圈圈里剩下的最后一个数字 ...
- 约瑟夫环(Josehpuse)的模拟
约瑟夫环问题: 0,1,...,n-1这n个数字排成一个圆圈,从数字0开始每次从这个圆圈里删除第m个数字,求出这个圆圈里剩下的最后一个数字. 这里给出以下几种解法, 1.用队列模拟 每次将前m-1个元 ...
- And Then There Was One(约瑟夫问题变形)
题目链接:http://poj.org/problem?id=3517 And Then There Was One Time Limit: 5000MS Memory Limit: 65536K ...
- Josephus环的四种解法(约瑟夫环)
约瑟夫环 约瑟夫环(约瑟夫问题)是一个数学的应用问题:已知n个人(以编号1,2,3…n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个 ...
随机推荐
- msp430入门学习21
msp430的USART的SPI模式 msp430入门学习
- POJ 1804 逆序对数量 / 归并排序
Brainman Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12175 Accepted: 6147 Descrip ...
- 记一次springMVC的跨域解决方案
日期:2019年5月18日 事情原因:由于微信小程序的开发只有测试环境,而后台提供借口的环境是开发环境:两个环境的域名不同,导致前端开发产生了跨域问题: 理论概念: 1.同源策略:同源策略是浏览器的安 ...
- JSP点击计数器
以下内容引用自http://wiki.jikexueyuan.com/project/jsp/hits-counter.html: 一个点击计数器能得知关于网站某个特定页面的访问量.假设人们第一次登陆 ...
- mssql存储过程异常处理
MSSQL2000和MSSQL2005以上版本的异常处理语法是不相同的. SQL Server 2005以上版本支持结构化异常处理,而MSSQL2000是不支持的. 1)先看MSSQL 2000的异常 ...
- WIN10中使用Hyper-V 配置虚拟机宿主机互ping
在Windows10 Hyper-V 中安装 Linux (Ubuntu16.04)虚拟机无法 ping 通宿主机,宿主机可以ping通虚拟机. 这种情况下关闭 Windows 防火墙就能ping通 ...
- Django学习系列之模板
什么是django模板 模板是一个文本,用于分离文档的表现形式和内容,模板定义了占位符以及各种用于规范文档该如何显示的各部分基本逻辑(模板标签) 模板通常用于产生HTML 如何使用模板 创建一个Tem ...
- quick-cocos2d-x游戏开发【1】——引擎结构总览和创建项目
好吧,我还是忍不住想写点关于quick的学习笔记,确实网上关于它的教程太少太少了,简单把自己的所学所得分享一下,有不正确之处还请拍砖. 首先下载引擎包.触控收购quick之后.如今cocos中文站的主 ...
- Java之旅hibernate(2)——文件夹结构
Hibernate的jar最好是到官网上进行下载.下载最新的稳定的版本号.之后进行解压,以下我们介绍一下hibernate的包结构. 1. 包结构 我们能够看到包文件夹结构发生了变化.我以5 ...
- Windows网络编程:winsock文件传输范例
基于TCP流协议的winsock网络文件传输Demo: 实现:C语言 功能:文件传输(可以传任何格式的文件) /******************************************** ...