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

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 nand k, where 5n
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 约瑟夫问题的变形,递推即可。
#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的更多相关文章
- 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 ...
- 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 ...
- Mac Pro 使用 ll、la、l等ls的别名命令
在 Linux 下习惯使用 ll.la.l 等ls别名的童鞋到 mac os 可就郁闷了~~ 其实只要在用户目录下建立一个脚本“.bash_profile”, vim .bash_profile 并输 ...
- Linux中的动态库和静态库(.a/.la/.so/.o)
Linux中的动态库和静态库(.a/.la/.so/.o) Linux中的动态库和静态库(.a/.la/.so/.o) C/C++程序编译的过程 .o文件(目标文件) 创建atoi.o 使用atoi. ...
- Mac OS使用ll、la、l等ls的别名命令
在linux下习惯使用ll.la.l等ls别名的童鞋到mac os可就郁闷了-- 其实只要在用户目录下建立一个脚本“.bash_profile”,并输入以下内容即可: alias ll='ls -al ...
- 水题 HDOJ 4727 The Number Off of FFF
题目传送门 /* 水题:判断前后的差值是否为1,b[i]记录差值,若没有找到,则是第一个出错 */ #include <cstdio> #include <iostream> ...
- .Uva&LA部分题目代码
1.LA 5694 Adding New Machine 关键词:数据结构,线段树,扫描线(FIFO) #include <algorithm> #include <cstdio&g ...
- 获取在线人数 CNZZ 和 51.la
string Cookies = string.Empty; /// <summary> /// 获取在线人数 (51.la统计器) /// </summary> /// &l ...
- BNU OJ 33691 / LA 4817 Calculator JAVA大数
留着当个模板用,在BNU上AC,在LA上RE……可能是java的提交方式不同??? 数和运算符各开一个栈. 表达式从左到右扫一遍,将数存成大数,遇到数压在 数的栈,运算符压在 运算符的栈,每当遇到右括 ...
随机推荐
- XAML(3) - 附带属性
WPF元素也可以从父元素中获得特性.例如,如果Button元素为了Canvas元素中,按钮的Top和Lef属性把父元素的名称作为前缀.这种属性成为附带属性: <Canvas> <Bu ...
- C# 将DataTable装换位List<T> 泛型
public List<T> GetList<T>(DataTable dt) where T:new() { List<T> DateLists = new Li ...
- Percona-Xtrabackup 2.3.3 慢查询依旧堵塞MariaDB备份(三)
MariaDB [yoon]> select version();+---------------------+| version() |+---------------------+| 10. ...
- hdu 5349 MZL's simple problem
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5349 MZL's simple problem Description A simple proble ...
- c++大数模板
自己写的大数模板,参考了小白书上的写法,只是实现了加减乘法,不支持负数,浮点数.. 除法还没写o(╯□╰)o以后再慢慢更吧.. 其实除法我用(xie)的(bu)少(lai),乘法写过fft,这模板还是 ...
- xUtils框架的介绍(一)
微信账号申请终于通过了,这是我们第一次Android干货分享. 想来是第一次,要对得起“干货”二字. 今天我要为大家推荐的是一个Android基于快速开发的一个框架——xUtils, 它是在aFina ...
- TFS使用指南
上一篇文章已经简略介绍过TFS的安装与管理,本篇文章主要描述一下我个人在工作过程中使用TFS的一些指南与建议.本章内容预览: 1. 项目计划与跟踪 经常有很多朋友在日常聊天中抱怨做计划很无畏,因为计 ...
- jQuery 获取 select 值和文本
jQuery("#select1").val();是取得选中的值, jQuery("#select1").text();就是取得的文本.
- android switch控件的使用
open.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Overridepublic void onCheckedChange ...
- 使用Log4j进行日志操作
使用Log4j进行日志操作 一.Log4j简介 (1)概述 Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台.文件.GUI组件.甚至是套接字服 ...