zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    SpringBoot 动态修改定时任务频率
    window三种程序自启动方式
    vbs与bat脚本实现本地jdk版本自动切换
    java连接sqlserver数据库
    java连接Access数据库
    Java如何连接Access数据库(两种方式实例代码)
    java连接access数据库的三种方式以及远程连接
    Linux下实现MySQL数据库每天定时自动备份
    解决谷歌浏览器http链接自动跳转到https的问题
    2021年第一天
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3723874.html
Copyright © 2011-2022 走看看