2014-04-30 16:12

题目链接

原题:

The beauty of a number X is the number of 1s in the binary representation of X. 

Two players are plaing a game. There is number n written on a black board. The game is played as following: 

Each time a player chooses an integer number ( <= k) so that ^k is less than n and (n-^k) has as beautiful as n.
Next he removes n from blackboard and writes n-^k instead.
The player that can not continue the game (there is no such k that satisfies the constrains) looses the game. The First player starts the game and they play in turns alternatively. Knowing that both two players play optimally you have to specify the winner. Input: First line of the Input contains an integer T, the number of testcase. <= T <= . Then follow T lines, each containing an integer n. n more than and less than ^ +. Output For each testcase print "First Player" if first player can win the game and "Second Player" otherwise. Sample Input Sample Output Second Player
First Player
First Player
Second Player
Second Player
First Player
Second Player Explanation In the first example n is and first player can't change it so the winner is the second player. In the second example n is , so the first player subtracts (^) from n and the second player looses the game.

题目:两人玩一个数字游戏,开始在黑板上写一个正整数。两人轮流对该数执行一种操作:将这个数换成一个更小的正整数,但得保证变化之后的数在二进制表示里具有相同个数的‘1’。比如5变到3就满足条件,两者的二进制里都有两个‘1’。如果轮到某人时,无法找到更小且满足条件的数,则此人输掉游戏。给定一个初始的数字n,请判断在双方都最优策略的情况下,谁一定会赢?有必胜策略吗?

解法:从题目里看这就像个博弈的题,于是我在错误的思路下进行了第一次尝试,最终明白这题似乎不用博弈。用一个例子来观察这种游戏规则:“100110”,这个数可以变成“10110”、“100101”。可以发现都是在“10”的地方变成了“01”,也就是把其中一个“1”向右移动一位。那么游戏何时结束?到了“111”就结束了,因为没有多余的“0”可供移动了。起点和终点都清楚了,并且每次只能移动一位,所以游戏的回合数其实是固定的,根据这个回合数的奇偶就知道谁输谁赢了。算法可以在O(log(n))时间内完成。

代码:

 // http://www.careercup.com/question?id=5110993575215104
#include <cstdio>
using namespace std; int main()
{
int nzero;
int sum;
int n; while (scanf("%d", &n) == && n > ) {
sum = ;
nzero = ;
while (n > ) {
if (n & ) {
sum += nzero;
} else {
++nzero;
}
n >>= ;
}
printf((sum & ) ? "First player wins.\n" : "Second player wins.\n");
} return ;
}

Careercup - Facebook面试题 - 5110993575215104的更多相关文章

  1. Careercup - Facebook面试题 - 6026101998485504

    2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...

  2. Careercup - Facebook面试题 - 5344154741637120

    2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...

  3. Careercup - Facebook面试题 - 5765850736885760

    2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...

  4. Careercup - Facebook面试题 - 5733320654585856

    2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...

  5. Careercup - Facebook面试题 - 4892713614835712

    2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...

  6. Careercup - Facebook面试题 - 6321181669982208

    2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...

  7. Careercup - Facebook面试题 - 5177378863054848

    2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...

  8. Careercup - Facebook面试题 - 4907555595747328

    2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...

  9. Careercup - Facebook面试题 - 5435439490007040

    2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...

随机推荐

  1. jquery实现简单的ajax

    -->html页 1: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...

  2. Houdini FX 14 重磅推出!(附下载方式)

    把之前发布在新浪的博客搬过来了,新浪广告太多,影响阅读和观感,博客园很清净~ SideFX于2015年1月在官网发布Houdini FX 14,喜爱尝鲜.充满好奇心的我迫不及待的装上Apprentic ...

  3. 每天一道LeetCode--374. Guess Number Higher or Lower

    We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...

  4. MySQL Server 5.5安装中遇到的问题及解决方法

    之前开始用师兄用过的电脑工作,各种不适应,开始重新搭建自己习惯的环境,重装MySQL就是其中一项,然后重装过程中遇到了问题,具体情况总结如下: 1.以前也重新安装过MySQL,但是完全没有用过就直接卸 ...

  5. Java之经典Student问题2

    创建5个学生对象给一个学生数组赋值,每个学生属性有:学号.姓名.年龄. 要求:1.给每个学生排序输出.  2.给所有学生年龄加1.  3.统计年龄大于20的学生人数. 在这里,我们用了compareT ...

  6. 利用kvc对UITabBar上的UITabBarButton的尝试修改.md

    一.前言 一次比较懒的想法,不想自定义UITabBar,也不想用第三方框架,于是想尝试修改苹果私有类来达到部分效果 效果如下 点击tabBar 上的按钮,图片有变大再变小的动画 tabBar 上某个按 ...

  7. Regsvr32注册ActiveX控件

    命令:Regsvr32 XX.dll 注册ActiveX控件 Regsvr32命令参数:/u 卸载ActiveX控件/s 注册成功后不显示操作成功信息框/c 控制台输出/I 调用DllInstall安 ...

  8. State模式

    地铁十字转门 状态迁移表格. 起始状态 触发迁移的事件 终止状态  要执行的动作. Locked   Coin               UnLocked UnLock UnLocked Pass  ...

  9. 《JavaScript高级程序设计》心得笔记-----第三篇章

    第十章 1.    DOM1级定义了一个Node接口,以Node类型实现(除IE以外),为了确保跨浏览器兼容,最好用nodeType属性与数字数值进行比较(someNode. nodeType==1) ...

  10. Typical sentences in SCI papers

       Beginning  1. In this paper, we focus on the need for   2. This paper proceeds as follow.   3. Th ...