UVA 1452 八 Jump

本文介绍了一种名为Jump的数列生成算法,该算法从圆周上按特定规则挑选数字形成序列,并提供了计算Jump序列最后三个数字的程序实现。具体而言,文章通过示例解释了如何构造Jump(n, k)序列,并给出了计算Jump序列最后三个数字的C语言程序代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Jump

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Submit  Status  Practice  UVA 1452

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 = 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$ \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
 1 #include <stdio.h>
 2 
 3 int fun(int m,int k,int i){
 4  
 5     if(i==1)
 6         return (m+k-1)%m;
 7     else
 8         return (fun(m-1,k,i-1)+k)%m;
 9  
10 }
11 
12 int main()
13 {
14      int T;
15      scanf("%d",&T);
16      while(T--)
17      {
18          int n,k,a,b,c;
19          scanf("%d %d",&n,&k);
20          if(k%6==1)
21              a=1,b=2,c=3;
22          if(k%6==2)
23              a=2,b=1,c=3;
24          if(k%6==3)
25              a=3,b=1,c=2;
26          if(k%6==4)
27              a=1,b=3,c=2;
28          if(k%6==5)
29              a=2,b=3,c=1;
30          if(k%6==0)
31              a=3,b=2,c=1;
32          a--,b--,c--;
33          for(int i=4;i<=n;i++)
34          {
35              a=(a+k)%i;
36              b=(b+k)%i;
37              c=(c+k)%i;
38          }
39          a++,b++,c++;
40          printf("%d %d %d\n",a,b,c);
41         //printf("%d %d %d\n",fun(n,k,n-2)+1,fun(n,k,n-1)+1,fun(n,k,n)+1);
42     }
43     return 0;
44 }
View Code

 

转载于:https://www.cnblogs.com/cyd308/p/4771582.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值