Знания по математике (3)

1. Принцип включения и исключения

#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;
}

2. Простая теория игр

  • Состояние первой руки должно победить: Вы можете перейти в определенное состояние поражения (Да)
  • Состояние поражения: если вы не можете перейти ни в одно состояние (Нет)
#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;
}

Supongo que te gusta

Origin blog.csdn.net/weixin_64443786/article/details/132118756
Recomendado
Clasificación