競技プログラミング日記

主に AtCoder の記事です

アルゴリズムと数学061 - Stone Game 2

アルゴリズムと数学061 - Stone Game 2

解法

勝敗を逆算していく. 手番を持っているほうが勝ちか負けかを考える.
今の局面が勝ちか負けかを決めるとき,

  • 遷移先に負けの局面が存在するならば,今の局面は勝ち.
  • 遷移先が全て勝ちの局面ならば,今の局面は負け.
  • それ以外は引き分け.

今回のゲームは,引き分けが存在しない.
1個のときに負け. 逆算すると, \(i\)個が負けならば \(2*i+1\)個が負け. それ以外は勝ち.

使っている記号,マクロ等 "https://ecsmtlir.hatenablog.com/entry/2022/12/23/131925"

int main() {
  ll n;
  cin >> n;

  vll lose;
  ll x = 1;
  while(x <= n){
    lose.push_back(x);
    x = 2*x + 1;
  }

  if(find(all(lose), n) != lose.end()){
    cout << "Second" << endl;
  }else {
    cout << "First" << endl;
  }
 
  return 0;
}