URAL 1180. Stone Game (博弈 + 规律)[通俗易懂]

URAL 1180. Stone Game (博弈 + 规律)

大家好,又见面了,我是全栈君。

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

Problem Author: Dmitry Filimonenkov


Problem Source: Third USU personal programming contest, Ekaterinburg, Russia, February 16, 2002

解析:找规律。

考虑例如以下样例:
剩余石子的数量    first Nikifor
1                              win
2                              win
—- 依次选择1,2. 而且1 和 2是能够选择的。

3                              lose
—- 由于你选1或2的时候,另外一个人总能一次把剩下的取完。

4, 5                          win

—- 依次选择1,2,可以获胜。

6                              lose

7, 8                          win

…. 从上面的规律我们能够看出当 N mod 3 == 0 我们能确认first Nikifor一定能失败;否则的话。我们能选择的最小石子的数量 = N mod 3,此时first Nikifor获胜。

AC代码:

#include <bits/stdc++.h>
using namespace std;

int main(){
    int ans = 0;
    char c;
    while(scanf("%c", &c) && c != '\n') ans += c - '0';     //求个位数字之和以推断数字能否被3整除,数太大。直接存不下!

if(ans % 3 == 0) puts("2"); else printf("%d\n%d", 1, ans % 3); return 0;}

.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116181.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • scrapyip池(ip route命令)

    目录一、中间件的使用1-1具体方法详解1-1-1process_request-正常请求调用1-1-2process_response-正常返回调用1-1-3process_exception-捕获错误调用二、Proxy相关官方中间件2-1HttpProxyMiddleware2-2RetryMiddleware2-2-1源码分析…

    2022年4月15日
    47
  • 落后的失利王朝死亡

    落后的失利王朝死亡

    2022年1月11日
    48
  • phpstorm 激活码_在线激活

    (phpstorm 激活码)2021最新分享一个能用的的激活码出来,希望能帮到需要激活的朋友。目前这个是能用的,但是用的人多了之后也会失效,会不定时更新的,大家持续关注此网站~https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~40…

    2022年3月28日
    54
  • 皮尔森相关系数(Pearson correlation coefficient)「建议收藏」

    皮尔森相关系数(Pearson correlation coefficient)「建议收藏」概述定义物理意义皮尔森距离机器学习中的应用代码实现概述皮尔森相关系数也称皮尔森积矩相关系数(Pearsonproduct-momentcorrelationcoefficient),是一种线性相关系数,是最常用的一种相关系数。记为r,用来反映两个变量X和Y的线性相关程度,r值介于-1到1之间,绝对值越大表明相关性越强。定义总体相关系数ρ定义为两…

    2022年4月20日
    598
  • mongovue查询字段_mongodb查询速度

    mongovue查询字段_mongodb查询速度{“ei”:”AW4BROILANDSTART1″,//条件一”cd”:{$elemMatch:{“0004″:{$gte:0}}}, //条件二,cd为集合,0004为集合中的key”st”:{$gte:ISODate(“2013-09-05T00:00:00.958Z”)}//时间条件,”im”:{$exists:true},”cn”:{$ne:””},

    2022年8月21日
    7
  • Qt Creator 安装 VLD

    Qt Creator 安装 VLD一 环境说明 1 VLD nbsp 内存检测工具 只能检测使用 VC 编译器 不能用于检测 MinGW 编译器 nbsp nbsp 所以要检测 nbsp Qt 内存泄露问题编译器一定要是 MSVC 环境要求 nbsp 1 VLD nbsp 版本要 2 X 以上 nbsp 不能使用 1 X 的版本 否则检测不准确 Qt 检测会提示很多内存泄露 本人使用 vld 2 3 setup exe nbsp 2 VC 编译器 nbsp 即 MSVC nbsp 如果有安装 VS 则就有这编译器 nbsp

    2025年12月6日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号