当前位置:网站首页>Inclusion exclusion principle (number divisible)

Inclusion exclusion principle (number divisible)

2022-06-13 11:02:00 I can screw the bottle cap when I am born again

 Insert picture description here

The number divisible

Given an integer n and m A different prime number p1,p2,…,pm.

Please find out 1∼n Middle energy quilt p1,p2,…,pm How many integers are divided by at least one number in .

Input format
The first line contains integers n and m.

The second line contains m A prime number .

Output format
Output an integer , Represents the number of integers that satisfy the condition .

Data range
1≤m≤16,
1≤n,pi≤109
sample input :
10 2
2 3
sample output :
7

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;

int n,m;
int a[20];
int main()
{
    
    ios::sync_with_stdio(false);
    cin>>n>>m;
    for (int i=0;i<m;i++) cin>>a[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*a[j]>n)
                {
    
                    t = -1;
                    break;
                }
                t*=a[j];
                s++;
            }
        }
        if (t!=-1)
        {
    
            if (s%2) res+=n/t;
            else
            res-=n/t;
        }
    }
    printf("%d",res);
    return 0;
}
原网站

版权声明
本文为[I can screw the bottle cap when I am born again]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206131056568827.html