数学知识(三)

一、容斥原理

#include<iostream>
#include<algorithm>

using namespace std;

const int N = 20;

typedef long long LL;
int n,m;
int p[N];

int main()
{
    cin>>n>>m;
    for(int i = 0;i < m;i ++) cin>>p[i];

    int res = 0;

    //从1枚举到2^m(位运算)
    for(int i = 1;i < 1 << m; i ++){

        //t表示当前质数的乘积
        //cnt表示当前选法中有几个集合
        int t = 1,cnt = 0;
        for(int j = 0;j < m;j ++)
        {
            //当前位为1
            if(i >> j & 1)
            {
                cnt ++;
                if((LL)t * p[j] > n)
                {
                    t = -1;
                    break;
                }
                t *= p[j];
            }
        }
        if(t != -1)
        {
            //奇数为加,偶数则减
            if(cnt % 2) res += n / t;
            else res -= n / t;
        }
    }
    cout<<res<<endl;
    return 0;
}

二、简单博弈论

  • 先手必胜状态:可以走到某一必败状态(Yes)
  • 先手必败状态:走不到任何一个必败状态(No)
#include<iostream>
#include<algorithm>

using namespace std;

int main()
{
    int n;
    in res = 0;
    
    scanf("%d",&n);
    
    while(n --)
    {
        int x;
        scanf("%d",&x);
        //异或
        res ^= x;
    }
    
    if(res) puts("Yes");
    else puts("No");
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_64443786/article/details/132118756
今日推荐