HIT OJ 1016 - Joseph‘s problem I

问题描述

Problem Description

The Joseph’s problem is notoriously known. For those who are not familiar with the problem, among n people numbered 1,2…n, standing in circle every mth is going to be executed and only the life of the last remaining person will be saved. Joseph was smart enough to choose the position of the last remaining person, thus saving his life to give the message about the incident.

Although many good programmers have been saved since Joseph spread out this information, Joseph’s cousin introduced a new variant of the malignant game. This insane character is known for its barbarian ideas and wishes to clean up the world from silly programmers. We had to infiltrate some the agents of the ACM in order to know the process in this new mortal game.

In order to save yourself from this evil practice, you must develop a tool capable of predicting which person will be saved.

The Destructive Process

The persons are eliminated in a very peculiar order; m is a dynamical variable, which each time takes a different value corresponding to the prime numbers’ succession (2,3,5,7…). So in order to kill the ith person, Joseph’s cousin counts up to the ith prime.

Input

It consists of separate lines containing n [1…3501], and finishes with a 0.

Output

The output will consist in separate lines containing the position of the person which life will be saved.

Sample Input

6

Sample Output

4

Hint

There are up to 50000 cases in one test, so please choose a faster algorithm.

问题分析

原来的约瑟夫问题,每次挑选要杀掉的人是使用一个固定的数字 n n n,这个问题里则使用了动态的数字,即质数序列

事先准备

质数序列求取 无需赘述,使用筛法,求取3501个质数备用

思路分析

最简单的办法自然是模拟,假设最初有 m m m个人,杀掉第 n n n个,剩下的人重新排序,算法复杂度 O ( n 2 ) O(n^2) O(n2)

假设,目前有 m m m个人,编号 1 , . . . . . . , m 1,......,m 1,......,m, 杀掉编号为 n n n的人,原来编号为 x x x的人(假设这个人没被干掉),下一轮的编号应该是什么?

原序列
1 , 2 , 3 , . . . . . , n − 1 , n , n + 1 , . . . . . , x , . . . . . . , m 1,2,3,.....,n-1,n,n+1,.....,x,......,m 1,2,3,.....,n1,n,n+1,.....,x,......,m

n n n被干掉后,新序列为
1 , . . . . . . , x − n , . . . . . . , m − n , 1 + ( m − n ) , 2 + ( m − n ) , 3 + ( m − n ) , . . . . . . , n − 1 + ( m − n ) 1,......,x-n,......,m-n,1+(m-n),2+(m-n),3+(m-n),......,n-1+(m-n) 1,......,xn,......,mn,1+(mn),2+(mn),3+(mn),......,n1+(mn)
显然有 x ′ = x + ( m − n ) % m (1) x' = x + ( m - n ) \% m\tag{1} x=x+(mn)%m(1)
而假设,有 m m m个人,杀掉第 l l l个人,其编号为 n n n,则
n = ( l % m ) i f ( l % m ! = 0 ) e l s e ( m ) (2) n = (l \% m) if(l\%m!=0) else (m) \tag{2} n=(l%m)if(l%m!=0)else(m)(2)

x ′ = ( x + m − l % m ) % m , l % m ! = 0 x ′ = ( x + m − m ) % m , l % m = 0 (3) x' = (x + m - l\%m)\%m, l \%m != 0\tag{3}\\ x' =(x+m-m)\%m,l\%m=0 x=(x+ml%m)%m,l%m!=0x=(x+mm)%m,l%m=0(3)
由(3)得,幸存下来的 x x x的新编号 x ′ x' x
x ′ = ( x + m − l % m ) % m (4) x'=(x+m-l\%m)\%m\tag{4} x=(x+ml%m)%m(4)
反推,若已知当前有 m − 1 m-1 m1个人,且上一轮幸存者 x x x在这一轮的编号为 x ‘ x‘ x,上一轮干掉了第 l l l个人,则
x ′ ′ = ( x ′ + l % m ) % m x = x ′ ′ , i f x ′ ′ > 0 , e l s e , m (5) x'' = ( x' + l\%m)\%m \\ x = x'' , if x'' > 0, else, m\tag{5} x=(x+l%m)%mx=x,ifx>0,else,m(5)

解法

这样一来,问题就简单了。
假设最初有 m m m个人, p r i m e i prime_i primei代表第 i i i个质数,最后留下来的人的编号肯定为 1 1 1,且上一轮人数为2,且上一轮干掉了第 p r i m e m − 1 prime_{m-1} primem1个人,则按照式(5)依次递推幸存者上次的编号。

Hint

根据hint中的提示,共有5000个case,但是可能的input范围为[1,3501],也就是说有很多case是重复的,所以可以开一个数组,存储当前计算后的值,下次直接从数组中取就可以了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值