LA 4727 jump(约瑟夫环变形)

Integers 1, 2, 3,..., n are placed on a circle in the increasing order as in the following figure. We want to construct a sequence from these numbers on a circle. Starting with the number 1, we continually go round by picking out each k-th number and send to a sequence queue until all numbers on the circle are exhausted. This linearly arranged numbers in the queue are calledJump(n,k) sequence where 1$ \le$n,k.

Let us compute Jump(10, 2) sequence. The first 5 picked numbers are 2, 4, 6, 8, 10 as shown in the following figure. And 3, 7, 1, 9 and 5 will follow. So we getJump(10, 2) = [2,4,6,8,10,3,7,1,9,5]. In a similar way, we can get easilyJump(13, 3) = [3,6,9,12,2,7,11,4,10,5,1,8,13],Jump(13, 10) = [10,7,5,4,6,9,13,8,3,12,1,11,2] andJump(10, 19) = [9,10,3,8,1,6,4,5,7,2].

\epsfbox{p4727.eps}

Jump(10,2) = [2,4,6,8,10,3,7,1,9,5]

You write a program to print out the last three numbers of Jump(n, k) for n, k given. For example suppose that n = 10,k = 2, then you should print 1, 9 and 5 on the output file. Note thatJump(1,k) = [1].

Input

Your program is to read the input from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case starts with a line containing two integersn andk, where5$ \le$n$ \le$500, 000 and 2$ \le$k$ \le$500, 000.

Output

Your program is to write to standard output. Print the last three numbers of Jump(n, k) in the order of the last third, second and the last first. The following shows sample input and output for three test cases.

Sample Input

3 
10 2 
13 10 
30000 54321

Sample Output

1 9 5 
1 11 2 
10775 17638 23432

 

 

约瑟夫环问题有两种解法:

1.直接进行模拟,很显然,对于这道题的数据范围来说,这种方法不能满足

2.数学方法

为了讨论方便,先把问题稍微改变一下,并不影响原意:
问题描述:n个人(编号0~(n-1)),从0开始报数,报到(m-1)的退出,剩下的人继续从0开始报数。求胜利者的编号。

我们知道第一个人(编号一定是m%n-1) 出列之后,剩下的n-1个人组成了一个新的约瑟夫环(以编号为k=m%n的人开始):
k k+1 k+2 ... n-2, n-1, 0, 1, 2, ... k-2并且从k开始报0。
现在我们把他们的编号做一下转换:

k --> 0
k+1 --> 1
k+2 --> 2
...
...
k-2 --> n-2
k-1 --> n-1
变换后就完完全全成为了(n-1)个人报数的子问题,假如我们知道这个子问题的解:例如x是最终的胜利者,那么根据上面这个表把这个x变回去不刚好就是n个人情况的解吗?!!变回去的公式很简单,相信大家都可以推出来:x'=(x+k)%n

个人感觉纯理论讲还是不太清楚,下面就举个例子说明(xn 代表 长度为n的那一行)

 n  =  6 ,k = 3

0,1,2,3,4,5     x6 = (x5 + 3)%6

3,4,  ,0,1,2     x5 = (x4 + 3)%5

0,1,  ,2,3        x4 = (x3 + 3)%4

1,2,     ,0        x3 = (x2 + 3)%3

1,        ,0         x2 = (x1 + 3)%2

0                     x1

从上面的式子可以看出来

x6的第二个数,是x5的第一个数、

x6的第三个数,是x4的第一个数,

x6的第四个数,是x3的第一个数,

x6的第五个数,是x2的第一个数,

x6的第六个数,是x1

由此可看出xn的第k个数,是x(n-k+1)的第一个数

而每个数列的第一个数(重新从0开始计数的第一个数)是可以直接的到的,再从这个数开始往上层层递推,变换回去,即可得到答案

 

计数时最好从0开始记,最后结果再加1就行了

如果从1开始记,注意取模运算会有0的问题

 

#include <stdio.h>
int main()
{
     int t;
     scanf("%d",&t);
     while(t--){
        int n, k, i, s;
        scanf("%d%d", &n, &k);
        s = (k - 1) % 3;
        for (i = 4; i <= n; i++)
        {
            s = (s + k) % i;
        }
        printf ("%d ", s+1);
        s = (k - 1) % 2;
        for (i = 3; i <= n; i++)
        {
            s = (s + k) % i;
        }
        printf ("%d ", s+1);
        s = 0;
        for (i = 2; i <= n; i++)
        {
            s = (s + k) % i;
        }
        printf ("%d\n", s+1);
     }
     return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值