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的那个 ...
随机推荐
- [K/3Cloud]将JSON字符串反序列化为C#动态对象
using Kingdee.BOS.Util; string errString="{/"Row/":1,/"PageId/":/"1234 ...
- 「CodePlus 2017 12 月赛」火锅盛宴
n<=100000种食物,给每个食物煮熟时间,有q<=500000个操作:在某时刻插入某个食物:查询熟食中编号最小的并删除之:查询是否有编号为id的食物,如果有查询是否有编号为id的熟食, ...
- Aizu - 0558 Cheese (bfs)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=49879 在H * W的地图上有N个奶酪工厂,分别生产硬度为1-N的奶酪.有一 ...
- 输入一个URL之后。。。
1.输入URL2.浏览器去浏览器缓存.系统缓存.路由器缓存查找缓存记录,有则直接访问URL对应的IP,无则下一步3.DNS解析URL,获得对应的IP4.浏览器通过TCP/IP三次握手连接服务器5.客户 ...
- RabbitMQ环境搭建教程收集(待实践)
先收集,后续再实践. http://blog.csdn.net/zyz511919766/article/details/41896823 http://blog.chinaunix.net/uid- ...
- mysql的时间戳说白了就俩问题,自动更新问题和不自动更新问题
mysql的时间戳timestamp说白了就俩问题,自动更新问题和不自动更新问题
- python的字典有些类似js对象
python的字典有些类似js对象 dict1 = {} dict1['one']= '1-one' dict1[2] = '2-tow' tinydict = {'name':'tome','cod ...
- [React] Build a slide deck with mdx-deck using Markdown + React
In this lesson we'll use mdx-deck to create a slide deck using Markdown and React. We'll look at add ...
- JS 获取form表单的所有数据
在HTML中用js获取通过GET.POST方法(就是在网址后加?a=b&c=d之类)传过来的表单值. 针对大家常用的获取表单方式,很多时候都是在重复的写一些代码,今天给大家贴出来的代码可以作为 ...
- DG备库磁盘空间满导致无法创建归档
上周五去某客户那里做数据库巡检.是window 2008系统上10g的一套NC系统的库,已经配置了DG,可是巡检时发现数据库报错: Tue Nov 11 10:13:57 2014 LNS: Stan ...