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 1nk.

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(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 nand k, where 5n500, 000 and 2k500, 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 约瑟夫问题的变形,递推即可。
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int MAX_N = 5e5 + ;
int N, K;
int dp1, dp2, dp3;
bool vis[]; void solve() {
for(int i = ; i <= N; ++i) {
int t = (K % i - + i) % i;
dp1 = (t + dp1 + ) % i;
dp2 = (t + dp2 + ) % i;
dp3 = (t + dp3 + ) % i;
} } int main()
{
// freopen("sw.in","r",stdin); int t;
scanf("%d", &t);
while(t--) {
scanf("%d%d", &N, &K);
memset(vis, , sizeof(vis));
dp1 = (K % + - ) % ;
dp2 = ( dp1 + (K % + - ) % + ) % ;
vis[dp1] = ;
vis[dp2] = ;
for(int i = ; i < ; ++i) if(!vis[i]) dp3 = i;
solve(); printf("%d %d %d\n",dp1 + , dp2 + , dp3 + );
}
//cout << "Hello world!" << endl;
return ;
}

LA 4727的更多相关文章

  1. leggere la nostra recensione del primo e del secondo

    La terra di mezzo in trail running sembra essere distorto leggermente massima di recente, e gli aggi ...

  2. Le lié à la légèreté semblait être et donc plus simple

    Il est toutefois vraiment à partir www.runmasterfr.com/free-40-flyknit-2015-hommes-c-1_58_59.html de ...

  3. Mac Pro 使用 ll、la、l等ls的别名命令

    在 Linux 下习惯使用 ll.la.l 等ls别名的童鞋到 mac os 可就郁闷了~~ 其实只要在用户目录下建立一个脚本“.bash_profile”, vim .bash_profile 并输 ...

  4. Linux中的动态库和静态库(.a/.la/.so/.o)

    Linux中的动态库和静态库(.a/.la/.so/.o) Linux中的动态库和静态库(.a/.la/.so/.o) C/C++程序编译的过程 .o文件(目标文件) 创建atoi.o 使用atoi. ...

  5. Mac OS使用ll、la、l等ls的别名命令

    在linux下习惯使用ll.la.l等ls别名的童鞋到mac os可就郁闷了-- 其实只要在用户目录下建立一个脚本“.bash_profile”,并输入以下内容即可: alias ll='ls -al ...

  6. 水题 HDOJ 4727 The Number Off of FFF

    题目传送门 /* 水题:判断前后的差值是否为1,b[i]记录差值,若没有找到,则是第一个出错 */ #include <cstdio> #include <iostream> ...

  7. .Uva&LA部分题目代码

    1.LA 5694 Adding New Machine 关键词:数据结构,线段树,扫描线(FIFO) #include <algorithm> #include <cstdio&g ...

  8. 获取在线人数 CNZZ 和 51.la

    string Cookies = string.Empty; /// <summary> /// 获取在线人数 (51.la统计器) /// </summary> /// &l ...

  9. BNU OJ 33691 / LA 4817 Calculator JAVA大数

    留着当个模板用,在BNU上AC,在LA上RE……可能是java的提交方式不同??? 数和运算符各开一个栈. 表达式从左到右扫一遍,将数存成大数,遇到数压在 数的栈,运算符压在 运算符的栈,每当遇到右括 ...

随机推荐

  1. 时隔一年,window.scroll

    function scrollToTop() { return function(btn, input) { var flag = false; var timer; function autoScr ...

  2. 刀哥多线程之调度组gcd-12-group

    调度组 常规用法 - (void)group1 { // 1. 调度组 dispatch_group_t group = dispatch_group_create(); // 2. 队列 dispa ...

  3. MIFARE系列6《射频卡与读写器的通讯》

    1. 复位应答(Answer to request) 读写器呼叫磁场内的卡片,卡片对呼叫做出应答.对刚进入磁场得到电复位处于休闲状态的卡片,卡请求(REQA,0x26):对于已进行过读写操作并进入休眠 ...

  4. EChart 关于图标控件的简单实用

    1.下载前段框架并放入项目中去. 2.在js中调用 <!DOCTYPE html> <html lang="en"> <head> <me ...

  5. 深度理解依赖注入(Dependence Injection)

    前面的话:提到依赖注入,大家都会想到老马那篇经典的文章.其实,本文就是相当于对那篇文章的解读.所以,如果您对原文已经有了非常深刻的理解,完全不需要再看此文:但是,如果您和笔者一样,以前曾经看过,似乎看 ...

  6. 在Linux上安装JDK7

    查看是否安装了JDK 如果安装完毕后,jdk版本不是当前所安装的,则需要卸载之前linux自带的jdk版本,因为安装Redhat9后默认安装了jdk, 可是默认安装的jdk1.4版本比较老,所以需要先 ...

  7. golang反射初试

    golang反射来自Go AST(Abstract Syntax Tree). reflect操作更多像traverse AST. t := reflect.TypeOf(obj) 使用TypeOf( ...

  8. ExtJs4学习MVC中的Store

    Ext.data.Store是extjs中用来进行数据交换和数据交互的标准中间件,无论是Grid还是ComboBox,都是通过它实现数据读取.类型转换.排序分页和搜索等操作的. 1 2 3 4 5 6 ...

  9. iOS学习之Object-C语言属性和点语法

    一.属性      1.属性的声明:在.h文件中使用@property声明属性.           @property NSString *name;       2.属性的作用是生成setter以 ...

  10. jsapi支付,提示redirect_uri 参数错误

    检查授权目录(微信支付——配置中心) appid MCHID KEYS 配置参数是否正确 appsecrect 配置是否正确(开发者中心) 如果是使用测试链接,需要同时指定测试授权目录,测试账号,并且 ...