日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Cow Digit Game(博弈论:sg函数)

發布時間:2024/9/3 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Cow Digit Game(博弈论:sg函数) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

鏈接:https://ac.nowcoder.com/acm/contest/1071/G
來源:牛客網

時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 32768K,其他語言65536K
64bit IO Format: %lld

題目描述

Bessie is playing a number game against Farmer John, and she wants you to help her achieve victory.
Game i starts with an integer Ni (1 <= Ni <= 1,000,000). Bessie goes first, and then the two players alternate turns. On each turn, a player can subtract either the largest digit or the smallest non-zero digit from the current number to obtain a new number. For example, from 3014 we may subtract either 1 or 4 to obtain either 3013 or 3010, respectively. The game continues until the number becomes 0, at which point the last player to have taken a turn is the winner.
Bessie and FJ play G (1 <= G <= 100) games. Determine, for each game, whether Bessie or FJ will win, assuming that both play perfectly (that is, on each turn, if the current player has a move that will guarantee his or her win, he or she will take it).
Consider a sample game where Ni = 13. Bessie goes first and takes 3, leaving 10. FJ is forced to take 1, leaving 9. Bessie takes the remainder and wins the game.

輸入描述:

  • Line 1: A single integer: G
  • Lines 2…G+1: Line i+1 contains the single integer: Ni

輸出描述:

  • Lines 1…G: Line i contains ‘YES’ if Bessie can win game i, and ‘NO’ otherwise.

示例1

輸入
復制

2 9 10

輸出
復制

YES NO

說明

For the first game, Bessie simply takes the number 9 and wins. For the second game, Bessie must take 1 (since she cannot take 0), and then FJ can win by taking 9.

思路

博弈論
本題最后取完者勝,
所以終態n=0為先取者的必敗點,
可以用sg函數來求解。

sg函數說明:

首先定義mex(minimal excludant)運算,這是施加于一個集合的運算,表示最小的不屬于這個集合的非負整數。例如mex{0,1,2,4}=3、mex{2,3,5}=0、mex{}=0。
對于任意狀態 x , 定義 SG(x) = mex(S),其中 S 是 x 后繼狀態的SG函數值的集合。如 x 有三個后繼狀態分別為 SG(a),SG(b),SG( c),那么SG(x) = mex{SG(a),SG(b),SG( c)}。 這樣 集合S 的終態必然是空集,所以SG函數的終態為 SG(x) = 0,當且僅當 x 為必敗點P時。
---------------from sg函數詳解傳送門

#include <iostream> #include <string.h> using namespace std; const int N = 1e6; int f[5],sg[N+2]; int getSg(int n) {if(sg[n]!= -1){return sg[n];}int minx = 10,maxx = -1;int x = n;while(x){int t = x%10;if(t){minx = min(minx,t);maxx = max(maxx,t);}x /= 10;}f[0] = getSg(n-minx);f[1] = getSg(n-maxx);for(int i = 0; ; i++){if(i==f[0]||i==f[1])continue;else{sg[n] = i;break;}}return sg[n]; } int main() {memset(sg,-1,sizeof(sg));sg[0] = 0;for(int i = 1; i <= N; i++){getSg(i);}int n;cin>>n;while(n--){int x;cin>>x;cout<<(sg[x]?"YES":"NO")<<endl;}return 0; }

總結

以上是生活随笔為你收集整理的Cow Digit Game(博弈论:sg函数)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。