HDU 1536 与 1944 S-Nim

这是一道典型的SG函数题;

View Code
 1 #include<iostream>

 2 #include<cstdio>

 3 #include<cstdlib>

 4 #include<algorithm>

 5 #include<cmath>

 6 #include<queue>

 7 #include<set>

 8 #include<map>

 9 #include<vector>

10 using namespace std;

11 int s[124],sg[10024],n;

12 int Getsg( int num )

13 {

14    int visit[124] = {0};

15    for( int i = 0 ; i < n ; i ++ )

16    {

17          int t = num - s[i];

18          if( t < 0 ) break;

19          if( sg[t] == -1 )

20              sg[t] = Getsg( t );

21          visit[sg[t]] = 1;      

22    } 

23    for( int i = 0; i < 101 ; i++ )

24        if( visit[i]==0 )

25            return i;    

26 }

27 int main(  )

28 {

29     int m,mm,num;

30     while( scanf( "%d",&n ),n )

31     {

32         for( int i = 0 ; i < n ; i ++ )

33              scanf( "%d",&s[i] );

34         sort( s , s + n );

35         scanf( "%d" ,&m );

36         memset( sg , -1, sizeof( sg ) ); 

37         sg[0] = 0 ;

38         while( m-- )

39         {

40             scanf( "%d",&mm );

41             int t = 0;

42             for( int i = 0 ; i < mm; i ++ )

43             {

44                 scanf( "%d",&num );

45                 if( sg[num]==-1 )

46                    sg[num] = Getsg( num ); 

47                 t ^= sg[num];     

48             }       

49             if( t == 0 ) putchar( 'L' );

50             else putchar( 'W' ); 

51         }  

52         puts( "" );     

53     }

54     //system( "pause" );

55     return 0;

56 }

 

你可能感兴趣的:(HDU)