Magic Beads

Magic Beads

Given number n, as the i-th \ (a_i \) , turns two operations, each operation may select a number \ (a_i \) , the (a_i \) \ submultiple of its own in addition to the game, then delete a divisor, then deleting \ (a_i \) , then select, this manner of operation, when a person can not be operated, then the player fails to win can be asked whether the upper hand, \ (. 1 <= n- <= 100,1 <= a_i <= 1000 \) .

solution

Note that this is icg, so naturally think of sg function, the number is not difficult to find a directed graph game, but there are a number of different stack produced about the view of a game, is a different situation, but the situation about the number of stacks of a number is a directed graph game, so we can triumph for whether a situation, take the situation there and the view of a game, and for a number of about (ie directed graph games) sg function values, as long as it generates each situation sg function values ​​can take mex operation, to other routines as a function of sg.

Reference Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#define il inline
#define ri register
using namespace std;
int a[101],dp[1001];
int SG(int);
int main(){
    int n;memset(dp,-1,sizeof(dp)),dp[1]=0;
    while(scanf("%d",&n)!=EOF){
        int ans(0);
        for(int i(1);i<=n;++i)
            scanf("%d",&a[i]),ans^=SG(a[i]);
        puts(ans?"freda":"rainbow");
    }
    return 0;
}
int SG(int x){
    if(dp[x]>=0)return dp[x];
    bool mex[1001];memset(mex,0,sizeof(mex));
    int d[1001],dt,i,j,k;d[dt=1]=1;
    for(i=2;i*i<x;++i)if(!(x%i))d[++dt]=i,d[++dt]=x/i;
    if(i*i==x)d[++dt]=i;
    for(i=1;i<=dt;mex[k]|=true,++i)
        for(j=1,k=0;j<=dt;++j)
            if(i!=j)k^=SG(d[j]);
    dp[x]=0;while(mex[dp[x]])++dp[x];
    return dp[x];
}

Guess you like

Origin www.cnblogs.com/a1b3c7d9/p/10948881.html
Recommended