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的那个 ...
随机推荐
- Spring data jpa 复杂动态查询方式总结
一.Spring data jpa 简介 首先我并不推荐使用jpa作为ORM框架,毕竟对于负责查询的时候还是不太灵活,还是建议使用mybatis,自己写sql比较好.但是如果公司用这个就没办法了,可以 ...
- 使用 docker 安装 OpenVAS 漏洞扫描软件
https://blog.csdn.net/freewebsys/article/details/78804624
- csu - 1536: Bit String Reordering (模拟)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1536 不知道为何怎么写都写不对. 这题可以模拟. 虽然题目保证一定可以从原串变成目标串,但是不一定 ...
- MongoDB学习day01--非关系型数据库
1.数据库和文件的主要区别: 1.1数据库有数据库表/行和列的概念,让我们存储操作数据方便 1.2数据库提供了方便的接口,让java.php..net.nodejs很方便的实现增删改查 2.NoSQL ...
- 采用jmeter和泛化测试dubbo服务接口
采用jmeter和泛化测试dubbo服务接口 http://blog.csdn.net/linuu/article/details/54313560
- phpunit 单元测试框架-代码覆盖率
"phpize not found" 的解决办法: apt-get install php5-dev http://jeffreysambells.com/2010/04/08/r ...
- Django学习系列之captcha 验证码插件
安装部署 安装captcha pip3. install django-simple-captcha== settings.py中引入captcha INSTALLED_APPS = [ 'djang ...
- NoSQL数据库概览及其与SQL语法的比較
[文章摘要] HBase是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统.同一时候也是知名的NoSQL数据库之中的一个.NoSQL数据库的产生就是为了解决大规模数据集合多重数据种类带来的挑战,尤 ...
- C语言++a与a++的实现机制与操作符结合优先级
看到一道"经典Linux C"面试题,关于左值和右值的. 华为笔试题 1.写出推断ABCD四个表达式的是否正确, 若正确, 写出经过表达式中 a的值(3分) int a = 4; ...
- javascript闭包具体解释
今天我们从内存结构上来解说下 javascript中的闭包概念. 闭包:是指有权訪问另外一个函数作用域中的变量的函数. 创建闭包的常见方式就是在一个函数内部创建另外一个函数. 在javascript中 ...