LA 4727

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 called Jump(nk) sequence where 1$ le$nk.

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 get Jump(10, 2) = [2,4,6,8,10,3,7,1,9,5]. In a similar way, we can get easily Jump(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] and Jump(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(nk) for nk given. For example suppose that n = 10k = 2, then you should print 1, 9 and 5 on the output file. Note that Jump(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 integers nand k, where 5$ 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(nk) 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 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 
 6 using namespace std;
 7 
 8 const int MAX_N = 5e5 + 7;
 9 int N, K;
10 int dp1, dp2, dp3;
11 bool vis[3];
12 
13 void solve() {
14         for(int i = 4; i <= N; ++i) {
15                 int t = (K % i - 1 + i) % i;
16                 dp1 = (t + dp1 + 1) % i;
17                 dp2 = (t + dp2 + 1) % i;
18                 dp3 = (t + dp3 + 1) % i;
19         }
20 
21 }
22 
23 int main()
24 {
25    // freopen("sw.in","r",stdin);
26 
27     int t;
28     scanf("%d", &t);
29     while(t--) {
30             scanf("%d%d", &N, &K);
31             memset(vis, 0, sizeof(vis));
32             dp1 = (K % 3 + 3 - 1) % 3;
33             dp2 = ( dp1 + (K % 2 + 2 - 1) % 2 + 1 ) % 3;
34             vis[dp1] = 1;
35             vis[dp2] = 1;
36             for(int i = 0; i < 3; ++i) if(!vis[i]) dp3 = i;
37             solve();
38 
39             printf("%d %d %d
",dp1 + 1, dp2 + 1, dp3 + 1);
40     }
41     //cout << "Hello world!" << endl;
42     return 0;
43 }
View Code
原文地址:https://www.cnblogs.com/hyxsolitude/p/3723874.html