【博弈】HackerRank _ stone_division
来源:互联网 发布:aloha软件 编辑:程序博客网 时间:2024/05/21 16:23
Consider the following game:
- There are two players, First and Second, sitting in front of a pile of stones. First always plays first.
- There is a set, , of distinct integers defined as .
- The players move in alternating turns. During each turn, a player chooses some and splits one of the piles into exactly smaller piles of equal size. If no exists that will split one of the available piles into exactly equal smaller piles, the player loses.
- Both players always play optimally.
Given , , and the contents of , find and print the winner of the game. If First wins, print First
; otherwise, print Second
.
Input Format
The first line contains two space-separated integers describing the respective values of (the size of the initial pile) and (the size of the set).
The second line contains distinct space-separated integers describing the respective values of .
Constraints
Output Format
Print First
if the First player wins the game; otherwise, print Second
.
Sample Input 0
15 35 2 3
Sample Output 0
Second
Explanation 0
The initial pile has stones, and . During First's initial turn, they have two options:
- Split the initial pile into equal piles, which forces them to lose after the following sequence of turns:
- Split the initial pile into equal piles, which forces them to lose after the following sequence of turns:
Because First never has any possible move that puts them on the path to winning, we print Second
as our answer.
/*啊我也不是很懂,看别人的代码补得题*/#include <bits/stdc++.h>using namespace std;typedef long long LL;LL a[15];int m;map<LL,int>mp;int check(LL n){ if(mp.count(n)) return mp[n]; int ans=0; for(int i=0;i<m;i++){ if(n%a[i]==0){ if(a[i]%2==0)/*能分成偶数块,先手必胜*/ return mp[n]=1; else ans|=!check(n/a[i]);/*在下一次先手必输的情况下,也就是对于这局来说后手必输的情况下,就是这局先手必胜的情况*/ } } return mp[n]=ans;}int main(){ LL n; scanf("%lld%d",&n,&m); for(int i=0;i<m;i++) scanf("%lld",&a[i]); printf(check(n)?"First\n":"Second\n"); return 0;}
- 【博弈】HackerRank _ stone_division
- Similar Pair _ HackerRank
- Cut the tree _ HackerRank
- hackerRank
- HackerRank
- HackerRank
- HackerRank
- HackerRank
- HackerRank
- HackerRank
- Hackerrank Week of Code 25 Stone Division(SG 博弈)
- 博弈_总结
- 硬币排成线_博弈
- 三国游戏_洛谷1199_博弈
- 什么博弈的_叫取石子
- HackerRank Palindromes
- Hackerrank && Network
- Hackerrank Pangrams
- [Sonatype Nexus]Nexus配置私有Maven库引入自定义jar包
- 线性分类器SVM(1)
- java提高篇(二)-----理解java的三大特性之继承
- Linux下I/O多路转接之epoll
- 预先定义ID,后面再使用
- 【博弈】HackerRank _ stone_division
- ssh 提示Connection closed by * 的解决方案
- 合并排序的递归实现算法
- socket编程——一个简单的例子
- Vue.js 快速搭建
- 飞机大战(五)
- POI分sheet导出Excel或者zip通用高速方法(万条数据以上)
- 整理整理
- VMware Workstation网络连接问题