这是 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的更多相关文章

  1. IEEEXtreme 10.0 - Game of Stones

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Game of Stones 题目来源 第10届IEEE极限编程大赛 https://www.hackerr ...

  2. IEEEXtreme 10.0 - Inti Sets

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Inti Sets 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank.c ...

  3. IEEEXtreme 10.0 - Painter's Dilemma

    这是 meelo 原创的 IEEEXtreme极限编程比赛题解 Xtreme 10.0 - Painter's Dilemma 题目来源 第10届IEEE极限编程大赛 https://www.hack ...

  4. IEEEXtreme 10.0 - Ellipse Art

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Ellipse Art 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank ...

  5. IEEEXtreme 10.0 - Counting Molecules

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Counting Molecules 题目来源 第10届IEEE极限编程大赛 https://www.hac ...

  6. IEEEXtreme 10.0 - Checkers Challenge

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Checkers Challenge 题目来源 第10届IEEE极限编程大赛 https://www.hac ...

  7. IEEEXtreme 10.0 - Playing 20 Questions with an Unreliable Friend

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Playing 20 Questions with an Unreliable Friend 题目来源 第1 ...

  8. IEEEXtreme 10.0 - Full Adder

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Full Adder 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank. ...

  9. IEEEXtreme 10.0 - N-Palindromes

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - N-Palindromes 题目来源 第10届IEEE极限编程大赛 https://www.hackerra ...

随机推荐

  1. js通过生成临时表单再删除的方式向后台提交数据(模拟ajax的post提交但还要跳转页面不返回数据)以及 struts向前台返回文件下载及防止中文乱码处理

    为了避免发送数据中有特殊字符,发送时用 encodeURIComponent 编码 (其实这个 if中是直接通过浏览器下载文件的方法,else是向后台传数据的方法) struts后台Action处理接 ...

  2. Listener 介绍

    当 web 应用在 web 容器中运行时,web 应用内部会不断地发生各种事件:如 web 应用启动.web 应用停止,用户 session 开始.用户 session 结束.用户请求到达等. 实际上 ...

  3. [DeeplearningAI笔记]序列模型1.7-1.9RNN对新序列采样/GRU门控循环神经网络

    5.1循环序列模型 觉得有用的话,欢迎一起讨论相互学习~Follow Me 1.7对新序列采样 基于词汇进行采样模型 在训练完一个模型之后你想要知道模型学到了什么,一种非正式的方法就是进行一次新序列采 ...

  4. 使用git上传项目到GitHub上

    之前的博客有<使用git拉取GitHub上的项目>的文章,那么现在说一下,如何上传项目到GitHub上. 1. Git的.gitignore 文档配置 因为项目中可能有很多的图片还有nod ...

  5. bzoj 3261: 最大异或和 (可持久化trie树)

    3261: 最大异或和 Time Limit: 10 Sec  Memory Limit: 512 MB Description       给定一个非负整数序列 {a},初始长度为 N.       ...

  6. 【BZOJ】2142 礼物

    [算法]中国剩余定理 [题意]给定n件物品分给m个人,每人分到wi件,求方案数%p.p不一定是素数. [题解] 首先考虑n全排列然后按wi划分成m份,然后对于每份内都是全排列,除以wi!消除标号影响, ...

  7. CodeForces - 1015D

    There are nn houses in a row. They are numbered from 11 to nn in order from left to right. Initially ...

  8. cookie知识点概述

    cookie是什么 这个讲起来很简单,了解http的同学,肯定知道,http是一个不保存状态的协议,什么叫不保存状态,就是一个服务器是不清楚是不是同一个浏览器在访问他,在cookie之前,有另外的技术 ...

  9. php上传文件大小限制的方法详解

    打开php.ini,首先找到file_uploads = on ;是否允许通过HTTP上传文件的开关.默认为ON即是开upload_tmp_dir ;文件上传至服务器上存储临时文件的地方,如果没指定就 ...

  10. Python3 多进程

    多进程(multiprocessing)的用法和多线程(threading)类似,里面的函数也一样,start()为启动函数,join() 等待该进程运行结束,每一个进程也是由它的父进程产生 1.简单 ...