1.容斥原理
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N= 20 ;
int p[ N] ;
int main ( )
{
int n, m;
cin>> n>> m;
for ( int i= 0 ; i< m; i++ ) cin>> p[ i] ;
int res= 0 ;
for ( int i= 1 ; i< 1 << m; i++ )
{
int t= 1 , s= 0 ;
for ( int j= 0 ; j< m; j++ )
if ( i>> j& 1 )
{
if ( ( ll) t* p[ j] > n)
{
t= - 1 ; break ;
}
t* = p[ j] ;
s++ ;
}
if ( t!= - 1 )
{
if ( s% 2 ) res+ = n/ t;
else res- = n/ t;
}
}
cout<< res<< endl;
return 0 ;
}
2.简单博弈论
#include <bits/stdc++.h>
using namespace std;
int main ( )
{
int n;
cin>> n;
int res= 0 ;
while ( n-- )
{
int x;
cin>> x;
res^ = x;
}
if ( res) puts ( "Yes" ) ;
else puts ( "No" ) ;
return 0 ;
}
3.台阶博弈论
#include <bits/stdc++.h>
using namespace std;
const int N= 100010 ;
int main ( )
{
int n;
scanf ( "%d" , & n) ;
int res= 0 ;
for ( int i= 1 ; i<= n; i++ )
{
int x;
cin>> x;
if ( i% 2 ) res^ = x;
}
if ( res) puts ( "Yes" ) ;
else puts ( "No" ) ;
return 0 ;
}
4.集合博弈论
#include <bits/stdc++.h>
using namespace std;
const int N= 110 , M= 10010 ;
int n, m;
int s[ N] , f[ M] ;
int sg ( int x)
{
if ( f[ x] != - 1 ) return f[ x] ;
unordered_set< int > S;
for ( int i= 0 ; i< m; i++ )
{
int sum= s[ i] ;
if ( x>= sum) S. insert ( sg ( x- sum) ) ;
}
for ( int i= 0 ; ; i++ )
if ( ! S. count ( i) ) return f[ x] = i;
}
int main ( )
{
cin>> m;
for ( int i= 0 ; i< m; i++ ) cin>> s[ i] ;
cin>> n;
memset ( f, - 1 , sizeof f) ;
int res= 0 ;
for ( int i= 0 ; i< n; i++ )
{
int x;
cin>> x;
res^ = sg ( x) ;
}
if ( res) puts ( "Yes" ) ;
else puts ( "No" ) ;
return 0 ;
}
5.拆分博弈论(与集合类似,只是构造sg函数方式不同)
#include <bits/stdc++.h>
using namespace std;
const int N= 110 , M= 10010 ;
int n;
int f[ M] ;
int sg ( int x)
{
if ( f[ x] != - 1 ) return f[ x] ;
unordered_set< int > S;
for ( int i= 0 ; i< x; i++ )
for ( int j= 0 ; j<= i; j++ )
S. insert ( sg ( i) ^ sg ( j) ) ;
for ( int i= 0 ; ; i++ )
if ( ! S. count ( i) ) return f[ x] = i;
}
int main ( )
{
cin>> n;
memset ( f, - 1 , sizeof f) ;
int res= 0 ;
for ( int i= 0 ; i< n; i++ )
{
int x;
cin>> x;
res^ = sg ( x) ;
}
if ( res) puts ( "Yes" ) ;
else puts ( "No" ) ;
return 0 ;
}