当前位置:网站首页>Mathematical knowledge: divisible number inclusion exclusion principle
Mathematical knowledge: divisible number inclusion exclusion principle
2022-07-03 01:27:00 【Fight! Sao Nian!】
subject :AcWing 890. 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
Topic analysis :
This question uses Principle of tolerance and exclusion
First of all, it can be used m Division of different prime numbers , Then maybe there will be 1、2、3… m Circumstances .
We use binary numbers to enumerate all cases
use S1 Represents the number of sets that can only be divided by one number ,S2 The number of sets that can be divided by two numbers …
According to the principle of inclusion and exclusion, all cases are set with odd number minus even number .
#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++) // Enumerate all the situations
{
int t=1,s=0; // t Represents the product of numbers that can be divisible ,s The number of numbers
for(int j=0;j<m;j++)
{
if(i>>j&1) // If selected
{
if((LL)t*p[j]>n) // If the selected number is greater than n
{
t=-1;
break;
}
t *= p[j];
s++;
}
}
if(t!=-1)
{
if(s%2)res+=n/t; // Odd plus
else res-=n/t; // Even minus
}
}
cout<<res<<endl;
return 0;
}
边栏推荐
- 不登陆或者登录解决oracle数据库账号被锁定。
- 按键精灵打怪学习-多线程后台坐标识别
- Canvas drawing -- bingdd
- The meaning of wildcard, patsubst and notdir in makefile
- 异步、邮件、定时三大任务
- The difference between tail -f, tail -f and tail
- Esp32 simple speed message test of ros2 (limit frequency)
- MySQL --- 数据库查询 - 条件查询
- 12_ Implementation of rolling automatic video playback effect of wechat video number of wechat applet
- Button wizard play strange learning - go back to the city to buy medicine and add blood
猜你喜欢

dotConnect for PostgreSQL数据提供程序

机器学习术语
![[androd] module dependency replacement of gradle's usage skills](/img/5f/968db696932f155a8c4a45f67135ac.png)
[androd] module dependency replacement of gradle's usage skills

C#应用程序界面开发基础——窗体控制(3)——文件类控件

Basic concept and implementation of overcoming hash

Androd Gradle 对其使用模块依赖的替换

Leetcode 2097 - Legal rearrangement of pairs

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

Esp32 simple speed message test of ros2 (limit frequency)

Correctly distinguish the similarities and differences among API, rest API, restful API and web service
随机推荐
按键精灵打怪学习-前台和内网发送后台验证码
Meibeer company is called "Manhattan Project", and its product name is related to the atomic bomb, which has caused dissatisfaction among Japanese netizens
LDC Build Shared Library
ThinkPHP+Redis实现简单抽奖
Now that the teenager has returned, the world's fireworks are the most soothing and ordinary people return to work~
How wide does the dual inline for bread board need?
Leetcode 6103 - minimum fraction to delete an edge from the tree
[system analyst's road] Chapter V double disk software engineering (development model development method)
看疫情之下服装企业如何顺势而为
dotConnect for PostgreSQL数据提供程序
Key wizard play strange learning - front desk and Intranet send background verification code
Androd Gradle 对其使用模块依赖的替换
[shutter] animation animation (the core class of shutter animation | animation | curvedanimation | animationcontroller | tween)
leetcode:871. 最低加油次数【以前pat做过 + 最大堆 +贪心】
1696C. Fishingprince Plays With Array【思维题 + 中间状态 + 优化存储】
对非ts/js文件模块进行类型扩充
Mongodb common commands of mongodb series
数学知识:Nim游戏—博弈论
18_ The wechat video number of wechat applet scrolls and automatically plays the video effect to achieve 2.0
Canvas drawing -- bingdd