求sg函数要先将集合中元素进行排序,然后一旦遇到大于当前的直接跳出会起到一定的优化,因为比打表省去了很多没用的求取,所以会快一些
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define MAX 107
using namespace std;
int n,m,a,t;
int sg[MAX*MAX];
int s[MAX];
int SG ( int m , int n )
{
if ( m == 0 ) return sg[0] = 0;
if ( sg[m] != -1 ) return sg[m];
bool hash[MAX];
memset ( hash , 0 , sizeof ( hash ) );
for ( int i = 1 ; s[i] <= m && i <= n ; i++ )
hash[SG(m-s[i], n)] = 1;
for ( int i = 0 ;; i++ )
if ( !hash[i] )
return sg[m] = i;
}
int main ( )
{
while ( ~scanf ( "%d" , &n ) , n )
{
for ( int i = 1 ; i <= n ; i++ ) scanf ( "%d" , &s[i] );
sort ( s+1 , s+n+1 );
scanf( "%d" , &t );
memset ( sg , -1 , sizeof ( sg ));
while ( t-- )
{
scanf ( "%d" , &m );
int ans = 0;
while ( m-- )
{
scanf ( "%d" , &a );
ans ^= SG ( a , n );
}
if ( ans ) printf ( "W" );
else printf ( "L" );
}
puts ("");
}
}