IEEEXtreme 10.0 - Flower Games
这是 meelo 原创的 IEEEXtreme极限编程比赛题解
题目来源 第10届IEEE极限编程大赛
https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/flower-games
Joy and her friends found a flower with N petals and want to play a modified version of the He loves me... he loves me not game. The girls number the petals with numbers from 1 to N in the clockwise direction. They will traverse the petals in circular order starting with 1, then 2, ..., then N, then 1... At the first petal, they will shout "He loves me", at the second "He loves me not" and tear it, at the third "He loves me", at the fourth "He loves me not" and tear it. The girls will continue the game until there is only one petal left. The task is to identify the number of the last petal.
Input Format
The input begins with an integer T, giving the number of test cases in the input.
Each testcase consists of an integer N, on a line by itself.
Constraints
1 <= T <= 100000
1 <= N < 2^63
Output Format
The location of the last petal, on a line by itself.
Sample Input
4
2
3
4
6
Sample Output
1
3
1
5
Explanation
There are four test cases in the input.
With 2 petals, one would skip 1, tear 2, and then only 1 is left.
With 3 petals, one would skip 1, tear 2, skip 3, tear 1, and then only 3 is left.
With 4 petals, one would skip 1, tear 2, skip 3, tear 4, skip 1, tear 3, and then only 1 is left.
With 6 petals, one would skip 1, tear 2, skip 3, tear 4, skip 5, tear 6, skip 1, tear 3, skip 5, tear 1, and then only 5 is left.
题目解析
这题是约瑟夫环问题。由于N的大小是2^63,肯定不能使用模拟的方法。
在这个问题里跳跃的距离总是1,是一种最为特殊的情况,我们不妨来找找规律。
2片花瓣,留下的是1号
3片花瓣,留下的是3号
4片花瓣,留下的是1号
5片花瓣,留下的是3号
6片花瓣,留下的是5号
7片花瓣,留下的是7号
8片花瓣,留下的是1号
9片花瓣,留下的是3号
10片花瓣,留下的是5号
很有规律是吧,结果总是个奇数。表示成二进制数,去掉最后1为就是:0,1,0,1,2,3,0,1,2,3,4,5,6,7。如果再在二进制数的第一位补上1,就变成了2,3,4,5,6,7,8,9,10,11,12,13,14,15。
问题就解决了。
其实这个问题可以给出证明。
由于2片花瓣,留下的是1号。
那么3片花瓣,去掉1瓣后,就变成了一个2片花瓣的问题,我们知道这个新问题留下的是1号。那么这个新问题的1号是原来的多少号呢,2x1+1=3号。
那么4片花瓣,去掉2瓣后,就变成了一个2片花瓣的问题,我们知道这个新问题留下的是1号。那么这个新问题的1号是原来的多少号呢,(2x2+1)%4=1号。
那么5片花瓣,去掉1瓣后,就变成了一个4片花瓣的问题,我们知道这个新问题留下的是1号。那么这个新问题的1号是原来的多少号呢,2x1+1=3号。
有2个幂片花瓣,最终留下的就是1号。
问题一般化后,N=(b1b2b3...bn)二进制,去掉(b2b3...bn)2后,就变成了一个(b100...0)片花瓣的问题,我们知道这个新问题留下的是1号。那么这个新问题的1号是原来的多少号呢,2x(b2b3...bn)2+1号。
程序
Java
import java.io.*;
import java.util.*; public class Solution { public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
for(int n = 0; n < N; n++) {
long input = scan.nextLong();
long highest = Long.highestOneBit(input);
long output = ((input & ~highest) << 1) + 1; System.out.println(output);
}
}
}
博客中的文章均为 meelo 原创,请务必以链接形式注明 本文地址
IEEEXtreme 10.0 - Flower Games的更多相关文章
- IEEEXtreme 10.0 - Game of Stones
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Game of Stones 题目来源 第10届IEEE极限编程大赛 https://www.hackerr ...
- IEEEXtreme 10.0 - Inti Sets
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Inti Sets 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank.c ...
- IEEEXtreme 10.0 - Painter's Dilemma
这是 meelo 原创的 IEEEXtreme极限编程比赛题解 Xtreme 10.0 - Painter's Dilemma 题目来源 第10届IEEE极限编程大赛 https://www.hack ...
- IEEEXtreme 10.0 - Ellipse Art
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Ellipse Art 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank ...
- IEEEXtreme 10.0 - Counting Molecules
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Counting Molecules 题目来源 第10届IEEE极限编程大赛 https://www.hac ...
- IEEEXtreme 10.0 - Checkers Challenge
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Checkers Challenge 题目来源 第10届IEEE极限编程大赛 https://www.hac ...
- IEEEXtreme 10.0 - Playing 20 Questions with an Unreliable Friend
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Playing 20 Questions with an Unreliable Friend 题目来源 第1 ...
- IEEEXtreme 10.0 - Full Adder
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Full Adder 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank. ...
- IEEEXtreme 10.0 - N-Palindromes
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - N-Palindromes 题目来源 第10届IEEE极限编程大赛 https://www.hackerra ...
随机推荐
- 【CodeChef】ForbiddenSum
Portal --> CC ForbiddenSum Solution 场上想到了\(O(NM)\)的做法..然而并没有什么用 首先考虑比较简单的一个问题,给定一个数组\(A\),问这些数不能凑 ...
- 框架----Django框架(基础篇)
一.基本配置 一.创建django程序 终端命令:django-admin startproject sitename IDE创建Django程序时,本质上都是自动执行上述命令 其他常用命令: pyt ...
- Android开发-eclipse+phonegap(Cordova)环境搭建
搭建步骤: 一.安装java [官网下载].eclipse+ADT+Android SDK [点我下载x86(android-22)] | [adt-bundle-windows-x86_64-201 ...
- C++引用C程序库函数
C与C++混合编程 C++里面如何声明const void f(void)函数为C程序中的库函数. void f(void)用c++ compiler来编译,在产生的obj文件中的名字变成了 $f@@ ...
- Qt ------ 判断运行在何种系统下
#ifdef Q_OS_WIN // Windows上的代码 #endif #ifdef Q_OS_LINUX // Linux上的代码 #endif #ifdef Q_OS_MAC // Mac上的 ...
- Bootstrap 按钮下拉菜单
向下拉 <div class="dropdown"> <button class="btn btn-default" data-toggle= ...
- Centos 修改limits.conf open files后不生效的解决办法
线上几台APACHE服务器报过三.四次open files的问题,导致服务不可用,执行ulimit -n 查看后,发现是默认的1024,找到原因所在了,就去修改下/etc/security/limit ...
- zTree使用技巧与详解
zTree--Jquery 树插件,是在后台管理页面中常使用到的插件. 使用效果图: 核心代码: zTree配置: var setting = { data:{simpleData:{enable:t ...
- UndertowServer+SpringMVC+Thymeleaf模板引擎构建轻量级的web项目
这两周需要写一个页面来请求另一个服务中的接口,服务器采用了超轻量级的undertow,模板引擎采用的是Thymeleaf,在寻找页面资源位置这个地方难住了我.下面分享一下,这方面的代码. Spring ...
- JAVA多线程提高五:原子性操作类的应用
当程序更新一个变量时,如果多线程同时更新这个变量,可能得到期望之外的值,比如变量i=1,A线程更新i+1,B线程也更新i+1,经过两个线程操作之后可能i不等于3,而是等于2.因为A和B线程在更新变量i ...