当前位置:网站首页>数学知识:能被整除的数—容斥原理
数学知识:能被整除的数—容斥原理
2022-07-03 01:03:00 【奋斗吧!骚年!】
题目:AcWing 890. 能被整除的数
给定一个整数 n 和 m 个不同的质数 p1,p2,…,pm。
请你求出 1∼n 中能被 p1,p2,…,pm 中的至少一个数整除的整数有多少个。
输入格式
第一行包含整数 n 和 m。
第二行包含 m 个质数。
输出格式
输出一个整数,表示满足条件的整数的个数。
数据范围
1≤m≤16,
1≤n,pi≤109
输入样例:
10 2
2 3
输出样例:
7
题目分析:
该题用到了容斥原理
首先能被m个不同的质数整除,那么可能就会有1、2、3… m个的情况。
我们使用二进制数来枚举所有情况
用S1表示只能被一个数整除的集合个数,S2能被两个数整除的集合个数…
由容斥原理得所有情况为加上奇数个数的集合减去偶数个数的集合。
#include <iostream>
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; // t表示能被整除的数的乘积,s表示数的个数
for(int j=0;j<m;j++)
{
if(i>>j&1) // 如果选取
{
if((LL)t*p[j]>n) // 如果选取的数大于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;
}
边栏推荐
- Data analysis, thinking, law breaking and professional knowledge -- analysis method (I)
- Every k nodes in the linked list are flipped
- 按鍵精靈打怪學習-多線程後臺坐標識別
- MySQL foundation 04 MySQL architecture
- Key wizard play strange learning - multithreaded background coordinate recognition
- dotConnect for PostgreSQL数据提供程序
- [shutter] animation animation (shutter animation type | the core class of shutter animation)
- 按键精灵打怪学习-自动寻路回打怪点
- tp6快速安装使用MongoDB实现增删改查
- R language ggplot2 visual faceting, visual facet_wrap bar plot, using strip Text function customize the size of the strip of each facet title in the facet graph (cutimi
猜你喜欢

看完这篇 教你玩转渗透测试靶机Vulnhub——DriftingBlues-9

Find a benchmark comrade in arms | a million level real-time data platform, which can be used for free for life
![[untitled]](/img/fd/f6b90536f10325a6fdeb68dc49c72d.png)
[untitled]

电话网络问题
![1696C. Fishingprince plays with array [thinking questions + intermediate state + optimized storage]](/img/bf/ab6838e34a3074130eac0a9992e77c.png)
1696C. Fishingprince plays with array [thinking questions + intermediate state + optimized storage]

【FPGA教程案例6】基于vivado核的双口RAM设计与实现

机器学习术语

MySQL - database query - basic query

dotConnect for PostgreSQL数据提供程序

Matlab saves the digital matrix as geospatial data, and the display subscript index must be of positive integer type or logical type. Solve the problem
随机推荐
看完这篇 教你玩转渗透测试靶机Vulnhub——DriftingBlues-9
Embrace the safety concept of platform delivery
Compare version number
tail -f 、tail -F、tailf的区别
What operations need attention in the spot gold investment market?
ThinkPHP+Redis实现简单抽奖
[system analyst's road] Chapter V double disk software engineering (development model development method)
对非ts/js文件模块进行类型扩充
Excel removes the data after the decimal point and rounds the number
按键精灵打怪学习-自动回城路线的判断
按鍵精靈打怪學習-多線程後臺坐標識別
Specified interval inversion in the linked list
Database SQL language 02 connection query
MySQL foundation 05 DML language
[自我管理]时间、精力与习惯管理
MySQL --- 数据库查询 - 条件查询
Arduino DY-SV17F自动语音播报
leetcode:701. Insertion in binary search tree [BST insertion]
Explain the basic concepts and five attributes of RDD in detail
Thinkphp+redis realizes simple lottery