UVA 1452 八 Jump

时间:2024-05-03 12:36:49
Jump

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

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 1UVA 1452	 八 Jumpnk.

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].

UVA 1452	 八 Jump
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 5UVA 1452	 八 JumpnUVA 1452	 八 Jump500, 000 and 2UVA 1452	 八 JumpkUVA 1452	 八 Jump500, 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
 #include <stdio.h>

 int fun(int m,int k,int i){

     if(i==)
return (m+k-)%m;
else
return (fun(m-,k,i-)+k)%m; } int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,k,a,b,c;
scanf("%d %d",&n,&k);
if(k%==)
a=,b=,c=;
if(k%==)
a=,b=,c=;
if(k%==)
a=,b=,c=;
if(k%==)
a=,b=,c=;
if(k%==)
a=,b=,c=;
if(k%==)
a=,b=,c=;
a--,b--,c--;
for(int i=;i<=n;i++)
{
a=(a+k)%i;
b=(b+k)%i;
c=(c+k)%i;
}
a++,b++,c++;
printf("%d %d %d\n",a,b,c);
//printf("%d %d %d\n",fun(n,k,n-2)+1,fun(n,k,n-1)+1,fun(n,k,n)+1);
}
return ;
}