1. 程式人生 > >Timus 1180. Stone Game 遊戲題目

Timus 1180. Stone Game 遊戲題目

Two Nikifors play a funny game. There is a heap ofNstones 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 numberN(conditionN≤ 10250holds).

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

這也是個有趣的問題,也很經典的遊戲題目的變形了。

不過這道題擴充套件了成為無限大的數了。

類似的遊戲有:沒人可以拿掉桌面上的棋子,每次不能超過5個,最後沒棋子可以拿的算輸

解決這樣的題目只能是尋找規律了,不能真的模擬區玩了,否則必定超時。

這道題目的規律就是:

1 如果給出的stone是3的倍數,那麼先取者必輸

2 如果給出的不是3的倍數,那麼先取者就湊成3的倍數就必贏,因為湊3的倍數很容易,去掉1個或者2個必定可以湊出來了

所以最後問題就成了mod3問題了。

我是怎麼想出來的?

我是一個列子一個例子去觀察,最後得出結論的,然後驗證,AC,結論正確。

也挺花時間的。

#include <string>
#include <iostream>
using namespace std;

int StoneGameMod3(string &s)
{
	int carry = 0;
	for (int i = 0; i < s.size(); i++)
	{
		int a = carry * 10 + s[i] - '0';
		carry = a % 3;
	}
	return carry;
}

void StoneGame1180()
{
	string s;
	cin>>s;
	int mod3 = StoneGameMod3(s);
	if (0 == mod3) cout<<2;
	else cout<<1<<endl<<mod3;
}

int main()
{
	StoneGame1180();
	return 0;
}


相關推薦

Timus 1180. Stone Game 遊戲題目

Two Nikifors play a funny game. There is a heap ofNstones in front of them. Both Nikifors in turns take some stones from the heap. One m

a simple stone game--k倍動態規劃減法遊戲

要求b[j],表示a[0]……a[i]組成,那麼顯然是要用到a[i]的,不然不就成了b[i-1],既然用了a[i],但是又要使相鄰的倍數在K以上。則找到最大的j,使a[j]*k<a[i]那麼滿足條件,便是a[0]……a[j]能組成的最大的數,加上a[i],那麼後者表示當前項不能和之前項組合,那麼最大的數

leetcode 877. 石子遊戲(Stone Game)

game 比賽 return 解法 n+1 clas public true urn 目錄 題目描述: 示例: 解法: 題目描述: 亞歷克斯和李用幾堆石子在做

[LintCode] Stone Game

begin win cnblogs stones player pla bsp merge panel There is a stone game.At the beginning of the game the player picks n piles of ston

uva 1378 - A Funny Stone Game sg博弈

1.0 mat cto i+1 clas typedef article 移動 post 題意:David 玩一個石子遊戲。遊戲中,有n堆石子,被編號為0..n-1。兩名玩家輪流取石子。 每一輪遊戲。每名玩家選取3堆石子i,j,k(i<j,j<=k,且至

POJ 1738:An old Stone Game 石子歸並 (GarsiaWachs算法)

scanf ++ namespace std score pre color bin not There is an old stone game.At the beginning of the game the player picks n(1<=n<=500

POJ 1740:A New Stone Game

always you 簡單 sum type down .text i++ class A New Stone Game Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5113

hdu 4388 Stone Game II

one put blog str ace while 不能 cas names Stone Game II HDU - 4388 題目大意: 給出n堆物品,每堆物品都有若幹件,現在A和B進行遊戲,每人每輪操作一次,按照如下規則: 1. 任意選擇一個堆,假設該堆有

hdu 1729 Stone Game

fin -- stream mes else targe tps gpo 遞歸 Stone Game HDU - 1729 題意: 給定n個箱子,每個箱子的容量為si,每個箱子裏最初有ci個石子,每次放入石子不能超過放入前的石子數的平方,誰無法繼續放入石子就算輸。

【POJ】1740.A New Stone Game

eof pac 博弈 turn += 多余 stream ring 就是 題解 想去學習一下博弈論的SG函數 不過貌似這道題就是猜結論並且證明 題意是,隨便選擇一堆石子,扔掉至少一個,然後從扔石子的這堆裏選擇任意多(可以不選)放到其他任意多的未選擇完的石堆裏 一堆石子,先手

POJ 1740 A New Stone Game(博弈)題解

const 證明 one int ios scan space return i++ 題意:有n個石子堆,每一個都可以輪流做如下操作:選一個石堆,移除至少1個石子,然後可以把這堆石子隨便拿幾次,隨便放到任意的其他石子數不為0的石子堆,也可以不拿。不能操作敗。 思路:我們先來

LeetCode 877. Stone Game(簡單DP)

Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. Example 1: Input: s1 = "sea", s2 = "eat"

hdu 4388 Stone Game II 博弈

題目連結:hdu 4388 參考部落格:https://blog.csdn.net/y1196645376/article/details/52143551 題意: 最初有n堆石子,每堆石子個數已知。兩人輪流執行操作,如果當某人無法執行有效操作時即輸。 操作分兩步: &nbs

A simple stone game HDU - 2486

文章目錄 A simple stone game HDU - 2486 題意: 解法: A simple stone game HDU - 2486 題意: 一堆石子有n個,兩個人輪流取,不能取的人輸。規則如

2018.11.07【POJ1740】A New Stone Game(階梯博弈)(模仿策略)

傳送門 解析: 首先,題目沒有說勝利判定方式啊,我去討論區看到勝利方式是拿走最後一顆石子。 這個博弈的主要策略是模仿。 每一方只需要保證對方有辦法可動的時候自己也有辦法可動就行了。 所以先手必敗的局面就是,有偶數堆石頭,並且每種大小的堆存在偶數個,因為只有這個時

877. 石子游戲 Stone Game

  亞歷克斯和李用幾堆石子在做遊戲。偶數堆石子排成一行,每堆都有正整數顆石子 piles[i] 。 遊戲以誰手中的石子最多來決出勝負。石子的總數是奇數,所以沒有平局。 亞歷克斯和李輪流進行,亞歷克斯先開始。 每回合,玩家從行的開始或結束處取走整堆石頭。 這種

[LeetCode] Stone Game

1、題目 Alex and Lee play a game with piles of stones.  There are an even number of piles arranged in a row, and each pile has a positiv

HDU 6237 A Simple Stone Game——思維

題意:有n(1e5)堆石子,第i堆石子有a[i](1e5)個,現在可以進行任意次操作,每次操作可以把一個石頭從一堆挪到另一堆,問最少操作幾次可以達成這個條件:存在一個x,使得每堆的石子數量都是x的倍數 思路:首先對石子總數進行質因分解,x一定是這些素因子中的一個,這個是我憑感覺蒙的,事實證明

Stone Game

ber tails con article mos i+1 提示 依次 輸入 Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a ro

Uva 1378 A Funny Stone Game (ACM ICPC 2006 Asia Regional Contest Beijing A Funny Stone Game)Nim 博弈

傳送門 王曉珂的題解乍一看非常難理解,且聽我慢慢解釋。 首先考慮這樣一個子問題: 當前棋子堆的編號為 m , 要求你把這個堆,並且放入兩堆編號分別小於 m 的棋子, 這裡分別計為 i, j (i, j < m) 那麼這種情況的 SG 是怎麼求的呢? 設當前編號 m 對應的局