timus 1180. Stone Game 解题报告

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 = 0;
    char tmp;
    while (cin >> tmp) s += tmp - ‘0‘;
    if (s % 3 == 0) cout << 2;
    else cout << 1 << ‘\n‘ << s % 3;
    //return 0;
}
时间: 2024-10-23 07:48:46

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

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 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.

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

最近CH上的比赛很多,在此会全部写出解题报告,与大家交流一下解题方法与技巧. T1 魔幻森林 描述 Cortana来到了一片魔幻森林,这片森林可以被视作一个N*M的矩阵,矩阵中的每个位置上都长着一棵树,其中一些树上结有能够产生能量的魔力水果.已知每个水果的位置(Xi,Yi)以及它能提供的能量Ci.然而,魔幻森林在某些时候会发生变化:(1) 有两行树交换了位置.(2) 有两列树交换了位置.当然,树上结有的水果也跟随着树一起移动.不过,只有当两行(列)包含的魔力水果数都大于0,或者两行(列)都没有魔

poj 2253 Frogger 解题报告

题目链接:http://poj.org/problem?id=2253 题目意思:找出从Freddy's stone  到  Fiona's stone  最短路中的最长路. 很拗口是吧,举个例子.对于 i 到 j 的一条路径,如果有一个点k, i 到 k 的距离 && k 到 j 的距离都小于 i 到 j 的距离,那么就用这两条中较大的一条来更新 i 到 j 的距离 .每两点之间都这样求出路径.最后输出 1 到 2 的距离(1:Freddy's stone   2:Fiona's sto

08年acm区域赛北京赛区 部分题解题报告

08年区域赛北京赛区 http://poj.org/searchproblem?field=source&key=Beijing+2008 POJ 3921 Destroying the bus stations 题目还是比较难的,当时的榜似乎只有4/25的通过/提交,其实题目数据很水.学长转换模型写了网络流求最小割,可以AC,不过自己造了个数据推翻了正确性.我写了个很挫的bfs套bfs,外层是最小的删除点数,内层是求最短路,数据很水可以AC.但比较蛋疼的在于bfs耗内存,而且队列中的点数是阶乘

NOIP2015 解题报告

过了这么久才来发解题报告,蒟蒻实在惭愧 /w\ Day1 T1 [思路] 模拟 [代码] 1 #include<iostream> 2 #include<cstring> 3 #include<queue> 4 #include<cmath> 5 #define FOR(a,b,c) for(int a=(b);a<=(c);a++) 6 using namespace std; 7 8 const int maxn = 50; 9 10 int G[

解题报告 之 ZOJ3877 Earthstone Keeper

解题报告 之 ZOJ3877 Earthstone Keeper Description Earthstone Keeper is a famous roguelike game created by Lizard Entertainment. In this game, an adventurer will explore in a single layer dungeon of N × M size. The adventurer starts at the room (SR, SC) an

解题报告 之 SOJ2414 Leapin&#39; Lizards

解题报告 之 SOJ2414 Leapin' Lizards Description Your platoon of wandering lizards has entered a strange room in the labyrinth you are exploring. As you are looking around for hidden treasures, one of the rookies steps on an innocent-looking stone and the

解题报告 之 POJ3057 Evacuation

解题报告 之 POJ3057 Evacuation Description Fires can be disastrous, especially when a fire breaks out in a room that is completely filled with people. Rooms usually have a couple of exits and emergency exits, but with everyone rushing out at the same time

hdu 1541 Stars 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 题目意思:有 N 颗星星,每颗星星都有各自的等级.给出每颗星星的坐标(x, y),它的等级由所有比它低层(或者同层)的或者在它左手边的星星数决定.计算出每个等级(0 ~ n-1)的星星各有多少颗. 我只能说,题目换了一下就不会变通了,泪~~~~ 星星的分布是不是很像树状数组呢~~~没错,就是树状数组题来滴! 按照题目输入,当前星星与后面的星星没有关系.所以只要把 x 之前的横坐标加起来就可以了