当前位置:网站首页>数学知识:能被整除的数—容斥原理
数学知识:能被整除的数—容斥原理
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;
}
边栏推荐
- 有向图的强连通分量
- 按键精灵打怪学习-前台和内网发送后台验证码
- 给你一个可能存在 重复 元素值的数组 numbers ,它原来是一个升序排列的数组,并按上述情形进行了一次旋转。请返回旋转数组的最小元素。【剑指Offer】
- R language ggplot2 visualization: use ggplot2 to display dataframe data that are all classified variables in the form of thermal diagram, and customize the legend color legend of factor
- 软考信息系统项目管理师_历年真题_2019下半年错题集_上午综合知识题---软考高级之信息系统项目管理师053
- Do not log in or log in to solve the problem that the Oracle database account is locked.
- 异步、邮件、定时三大任务
- d. LDC build shared library
- Look at how clothing enterprises take advantage of the epidemic
- 【FH-GFSK】FH-GFSK信号分析与盲解调研究
猜你喜欢

寻找标杆战友 | 百万级实时数据平台,终身免费使用

软考信息系统项目管理师_历年真题_2019下半年错题集_上午综合知识题---软考高级之信息系统项目管理师053

MySQL foundation 04 MySQL architecture

Data analysis, thinking, law breaking and professional knowledge -- analysis method (I)

Leetcode 2097 - Legal rearrangement of pairs

12_ Implementation of rolling automatic video playback effect of wechat video number of wechat applet

异步、郵件、定時三大任務

MySQL

leetcode:871. 最低加油次数【以前pat做过 + 最大堆 +贪心】

一比特苦逼程序員的找工作經曆
随机推荐
leetcode:701. 二叉搜索树中的插入操作【bst的插入】
Linear programming of mathematical modeling (including Matlab code)
leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]
Excel removes the data after the decimal point and rounds the number
1696C. Fishingprince plays with array [thinking questions + intermediate state + optimized storage]
[FPGA tutorial case 5] ROM design and Implementation Based on vivado core
FPGA - 7 Series FPGA internal structure clocking -04- multi area clock
【第29天】给定一个整数,请你求出它的因子数
[C language] branch and loop statements (Part 1)
Dotconnect for PostgreSQL data provider
测试右移:线上质量监控 ELK 实战
无向图的割点
leetcode:701. Insertion in binary search tree [BST insertion]
R language generalized linear model function GLM, (model fit and expression diagnostics), model adequacy evaluation method, use plot function and car package function
攻克哈希的基本概念与实现
[my advanced journey of OpenGL learning] collation of Euler angle, rotation order, rotation matrix, quaternion and other knowledge
kivy教程之在 Kivy App 中使用 matplotlib 的示例
Key wizard play strange learning - front desk and Intranet send background verification code
Embrace the safety concept of platform delivery
有向图的强连通分量