1.题目:

1180. Stone Game

Time limit: 1.0 second
Memory limit: 64 MB
Two Nikifors play a funny game. There is a heap of N stones in front of them. Both Nikifors in turns take some stones from the heap. One may take any number of stones with the only condition that this number is a nonnegative integer power of 2 (e.g. 1, 2, 4, 8 etc.). Nikifor who takes the last stone wins. You are to write a program that determines winner assuming each Nikifor does its best.

Input

An input contains the only positive integer number N (condition N ≤ 10^250 holds).

Output

The first line should contain 1 in the case the first Nikifor wins and 2 in case the second one does. If the first Nikifor wins the second line should contain the minimal number of stones he should take at the first move in order to guarantee his victory.

Sample

input output
8
1
2

2.解题思路

这种博弈问题,都是从最简单的情况考虑,递推到复杂情况的。但是这道题有一些有趣的技巧~

基本的递推:

N  win?

1  y

2  y

3  n

嗯。分析到这里,思路大概是这样的:

bool suc[maxN+1];

suc[1] = suc[2] = true;

for (i = 3; i <= n; i++) {

  for (b = 1; b < n; b *= 2) {

    if (suc[i-b] == false) {

      suc[i] = true;

      break;

    }

  }

}

然而再回头看一下题目规模,发现n的取值范围是n <= 10^250。这是要高精度的节奏啊。

不急,回去再看一下有没有优化的方法。通过找规律,

发现:N = 3*n, suc[n] = n

   N = 3*n + 1 || N = 3*n + 2, suc[n] = y

这样问题转换为求n%3。

而一个十进制数N = sum(ai*10^i) = sum(ai) + sum(ai*(10^i-1))(i>=0)

而10^i-1 = 0 (mod3)

故N = sum(ai) (mod3)

3.代码:

#include <iostream>
using namespace std;
int main()
{
int s = ;
char tmp;
while (cin >> tmp) s += tmp - '';
if (s % == ) cout << ;
else cout << << '\n' << s % ;
//return 0;
}

timus 1180. Stone Game 解题报告的更多相关文章

  1. 【九度OJ】题目1180:对称矩阵 解题报告

    [九度OJ]题目1180:对称矩阵 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1180 题目描述: 输入一个N维矩阵,判断是否对称 ...

  2. 【LeetCode】1046. Last Stone Weight 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 大根堆 日期 题目地址:https://leetco ...

  3. timus 1175. Strange Sequence 解题报告

    1.题目描述: 1175. Strange Sequence Time limit: 1.0 secondMemory limit: 2 MB You have been asked to disco ...

  4. Timus 1180. Stone Game 游戏题目

    Two Nikifors play a funny game. There is a heap of N stones in front of them. Both Nikifors in turns ...

  5. 【LeetCode】877. Stone Game 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学 双函数 单函数 + 记忆化递归 动态规划 日期 ...

  6. CH Round #56 - 国庆节欢乐赛解题报告

    最近CH上的比赛很多,在此会全部写出解题报告,与大家交流一下解题方法与技巧. T1 魔幻森林 描述 Cortana来到了一片魔幻森林,这片森林可以被视作一个N*M的矩阵,矩阵中的每个位置上都长着一棵树 ...

  7. 【NOIP2015】提高day2解题报告

    题目: P1981跳石头 描述 一年一度的“跳石头”比赛又要开始了!这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石.组委会已经选择好了两块岩石作为比赛起点和终点.在起点和终点之间,有 N ...

  8. NOIP2015 提高组(senior) 解题报告

    过了这么久才来发解题报告,蒟蒻实在惭愧 /w\ Day1 T1 [思路] 模拟 [代码] #include<iostream> #include<cstring> #inclu ...

  9. $HNOI\ 2010$ 解题报告

    HNOI 2010 解题报告 0. HNOI2010 AC代码包下载地址 注: 戳上面的标题中的'地址' 下载 代码包, 戳下面每一题的文件名 可进入 题目链接. 每一题 对应代码的文件名 我在 每一 ...

随机推荐

  1. HDU 1281 二分图

    棋盘游戏 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  2. Junit测试 - Spring的配置

    第一种: @ContextConfiguration(locations = {"classpath*:/spring-core.xml"}) public class UserM ...

  3. DBUtils

    DBUtils中核心对象 > QueryRunner类 它提供了操作数据增删改查的方法 query() 执行select语句的 update() 执行insert update delete 语 ...

  4. EMF学习,为了实现可扩展可自定义的模型验证 - 各种实现方法学习

    自: http://blog.csdn.net/javaman_chen/article/details/6057033 http://www.ibm.com/developerworks/cn/op ...

  5. hadoop意外之旅--巧合遇到一只大象

    公司面临转型,所有开发也难免面临转型,开始选择自己想要走的方向进行研究. 说来也巧合,最近正好说搭个hadoop环境玩玩,结果遇到转型还被选为大数据小组组长.(尴尬) 开始一场遇到大象之旅,希望能在这 ...

  6. Xamarin.iOS Unified API 注意要点

    新数据类型 NATIVE TYPE 32-BIT BACKING TYPE 64-BIT BACKING TYPE System.nint System.Int32 (int) System.Int6 ...

  7. pt-archiver使用

    pt-archiver工具其实就是用来清理,归档数据用的 一.归档前的准备需要配置client字符集为utf-8,如果你用了utf-8的编码,防止归档数据为乱码[client]default-char ...

  8. 别再为了iOS新系统设备而重新安装一个新版Xcode了.其实我们可以添加版本支持

    众所周知,Xcode7.3的代码补全是有问题的  如导入自定义类之后,在代码中并不会补全相应的类名... 但Xcode7.2是没有这个问题的,但很多时候我们自己的设备都升级到了iOS9.3.X系统,导 ...

  9. Oracle数据库基础知识2

    字符操作相关_1 1.CONCAT关键字作用:连接字符串语法:CONCAT(字串1, 字串2)例如: CONCAT('hello','world') FROM DUAL; 注意:Oracle的CONC ...

  10. SQL Server 2014 Backup Encryption

    转载自: Microsoft MVP Award Program Blog 来源:Microsoft MVP Award Program Blog 的博客:https://blogs.msdn.mic ...