Hackerrank Week of Code 25 Stone Division(SG 博弈)
来源:互联网 发布:跳跃网络 中青宝 编辑:程序博客网 时间:2024/05/16 07:54
题意:有n个石子的堆,两人轮流游戏,有m个数的集合,每个人只能在集合中选一个数si,然后把n个石子严格平分成si堆,若某人不能严格平分的时候为败。
(n <= 1e18, m <= 10)思路:暴力求SG, 用map进行记忆化存SG值,SG[n] = 0的时候,先手必败,否则必胜。n个石子分成si个n/si大小的堆,SG[n]等于si个SG[n/si]相XOR,因为XOR满足结合律所以可以这样处理。最后map里面存的都是n的因数,数学可证因数的个数复杂度为O(n ^ 1/3), 所以总的算法复杂度为 O(n ^ 1/3 * m * logn)而且此题都是相同的SG值XOR,所以只要记录SG是否为0用bool记录就可以了。
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <iostream> #include <string> #include <cmath> #include <vector> #include <set> #include <map> #include <bitset> #include <stack> using namespace std; #define REP(i,n) for ( int i=1; i<=int(n); i++ ) #define MP make_pair #define PB push_back #define SZ(x) (int((x).size())) #define ALL(x) (x).begin(), (x).end() #define X first #define Y second template<typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } template<typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } typedef long long LL; typedef long double LD; const int INF = 0x3f3f3f3f; map<LL, bool> sg;LL n, m;LL a[15];bool grundy(LL num) {if(sg.count(num)) return sg[num];bool &ans = sg[num];for(int i = 1; i <= m; i ++) {if(num % a[i]) continue;if(a[i] & 1) ans |= (grundy(num / a[i]) == 0);else ans |= 1;}return ans;}int main() {cin >> n >> m;for(int i = 1; i <= m; i ++) cin >> a[i];if(grundy(n)) puts("First");else puts("Second");}
Submissions will no longer be placed on the leaderboard. You may still attempt this problem for practice.
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
15 35 2 3
Sample Output
Second
Explanation
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.
- Hackerrank Week of Code 25 Stone Division(SG 博弈)
- hackerrank Week of Code Target
- [HackerRank]Week Of Code 23
- Hackerrank Week of Code 32
- HackerRank Week of Code -18 GG
- HackerRank——Week of Code 26
- Hackerrank——Week of Code 27
- Hackerrank——Week of Code 29
- [Hackerrank Week of Code 30]Range Modular Queries
- [Hackerrank Week of Code 30]A Graph Problem
- Hackerrank Week of Code 30 Range Modular Queries
- [HackerRank Week of Code]DAG Queries/[JZOJ5038]命运的构造
- [后缀自动机][分块]HackerRank Week of Code 30 .Substring Queries
- 【lightOJ】 Again Stone Game (博弈,SG函数)
- 解题报告:HDU1729 Stone Game SG博弈
- UVA 11859 - Division Game (SG博弈)
- HackerRank Week of Code 23——Enclosure(等周定理+余弦定理)
- 【后缀自动机+阈值优化】HackerRank(Week of Code 30)[Substring Queries]题解
- MagicButton
- C++标准库 阅读笔记
- (1)构建OpenGL ES运行环境
- Linux flash 文件系统剖析【转】
- css无刷新改变地址栏参数
- Hackerrank Week of Code 25 Stone Division(SG 博弈)
- iOS10的推送框架UserNotifications
- 将字符串转化为数字与将数字转化为字符串
- Spring定义bean的三种方式和自动注入
- Android 平台下的原生 Markdown 解析器
- android 间 通信
- Zabbix
- 【MyBatis】——自增主键与非自增主键返回
- 安卓数据库封装