Hackerrank Week of Code 25 Stone Division(SG 博弈)

题意:有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   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
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 inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }  
template 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 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 3
5 2 3

Sample Output

Second

Explanation

The initial pile has  stones, and . During First's initial turn, they have two options:

  1. Split the initial pile into  equal piles, which forces them to lose after the following sequence of turns: 
    Hackerrank Week of Code 25 Stone Division(SG 博弈)_第1张图片
  2. Split the initial pile into  equal piles, which forces them to lose after the following sequence of turns: 
    Hackerrank Week of Code 25 Stone Division(SG 博弈)_第2张图片

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 博弈))