`
Simone_chou
  • 浏览: 195795 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

Jump(递推)

    博客分类:
  • UVA
 
阅读更多

4727 - Jump

Asia - Seoul - 2009/2010

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(n, k) sequence where 1 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 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].

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 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 n and k,

where 5 n 500, 000 and 2 k 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

 

         题意:

         约瑟夫环问题。给出 N 和 M,输出最后输出的三个数是什么。

 

         思路:

         递推。a,b,c 保存最后三个的数是什么。

 

         AC:

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int main() {
        int t;
        scanf("%d", &t);

        while (t--) {
                int n, m;
                int a, b, c;
                scanf("%d%d", &n, &m);

                if (m % 6 == 1) a = 1, b = 2, c = 3;
                if (m % 6 == 2) a = 2, b = 1, c = 3;
                if (m % 6 == 3) a = 3, b = 1, c = 2;
                if (m % 6 == 4) a = 1, b = 3, c = 2;
                if (m % 6 == 5) a = 2, b = 3, c = 1;
                if (m % 6 == 0) a = 3, b = 2, c = 1;

                for (int i = 4; i <= n; ++i) {
                        a = !((a + m) % i) ? i : (a + m) % i;
                        b = !((b + m) % i) ? i : (b + m) % i;
                        c = !((c + m) % i) ? i : (c + m) % i;
                }

                printf("%d %d %d\n", a, b, c);
        }

        return 0;
}

 

分享到:
评论

相关推荐

    台阶问题(洛谷-P1192).rar

    这个问题的核心在于找到一个递推关系或者动态规划的解决方案。 在编程中,动态规划是一种有效的方法来解决这类问题。动态规划通过将大问题分解为子问题,并存储子问题的解,避免了重复计算,从而提高了效率。对于...

    清华大学C语言教学共16个第7个PPT学习教案.pptx

    5. **递推关系**:通过对不同荷叶数量的情况进行分析,我们可以发现`Jump(0,y)`与荷叶数量`y`之间存在递推关系。在没有柱子的情况下,过河的青蛙数等于荷叶数加1,即`Jump(0,y) = y + 1`。这展示了如何通过观察和...

    leetcode1-300.docx

    10. 动态规划的题目分为两大类,一种是求最优解类,典型问题是背包问题,另一种就是计数类,比如这里的统计方案数的问题,它们都存在一定的递推性质。 这个内容讲解了动态规划的两大类题目,包括求最优解类和计数类...

    具有(x,u,v)相关噪声的离散时间Markov跳跃系统的无穷H2 /H∞最优控制

    9. 后向迭代算法(Backward Iterative Algorithm)是一种递推算法,它通过从后往前逐步迭代计算,可以用来求解特定类型的数学问题,如矩阵Riccati方程。 10. 精确可检测性(Exact Detectability)是指系统状态能够...

    2021数学建模五一赛A题《疫苗生产问题》

    模拟仿真生产车间的生产操作,在满足疫苗按 4 个工位依次生产、每个工位不能同时生产不同类型疫苗、疫苗生产不允许插队以及允许同一个疫苗的四个工位可不连续工作的条件下,通过枚举和递推算法编程求解目标问题的最...

Global site tag (gtag.js) - Google Analytics