当前位置:网站首页>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;
}
边栏推荐
- Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
- 1696C. Fishingprince Plays With Array【思维题 + 中间状态 + 优化存储】
- Is there anything in common between spot gold and spot silver
- The industrial scope of industrial Internet is large enough. The era of consumer Internet is only a limited existence in the Internet industry
- Basis of information entropy
- 1696C. Fishingprince plays with array [thinking questions + intermediate state + optimized storage]
- [FPGA tutorial case 5] ROM design and Implementation Based on vivado core
- Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
- R language uses coin package to apply permutation tests to independence problems (permutation tests, whether response variables are independent of groups, are two numerical variables independent, and
- 信息熵的基础
猜你喜欢
![[androd] module dependency replacement of gradle's usage skills](/img/5f/968db696932f155a8c4a45f67135ac.png)
[androd] module dependency replacement of gradle's usage skills

力扣 204. 计数质数

MySQL - database query - basic query

1696C. Fishingprince Plays With Array【思维题 + 中间状态 + 优化存储】

MySQL - database query - condition query
![leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]](/img/2c/8ec3926243fac8db9ed45d8053f3af.png)
leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]

【我的OpenGL学习进阶之旅】关于欧拉角、旋转顺序、旋转矩阵、四元数等知识的整理

Strongly connected components of digraph

leetcode 6103 — 从树中删除边的最小分数

leetcode:871. 最低加油次数【以前pat做过 + 最大堆 +贪心】
随机推荐
[shutter] animation animation (the core class of shutter animation | animation | curvedanimation | animationcontroller | tween)
Meibeer company is called "Manhattan Project", and its product name is related to the atomic bomb, which has caused dissatisfaction among Japanese netizens
Database SQL language 02 connection query
[my advanced journey of OpenGL learning] collation of Euler angle, rotation order, rotation matrix, quaternion and other knowledge
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
按键精灵打怪学习-自动寻路回打怪点
Makefile中wildcard、patsubst、notdir的含义
[self management] time, energy and habit management
What operations need attention in the spot gold investment market?
ThinkPHP+Redis实现简单抽奖
Key wizard hit strange learning - automatic path finding back to hit strange points
Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
MySQL --- 数据库查询 - 基本查询
How is the mask effect achieved in the LPL ban/pick selection stage?
Kivy tutorial how to create drop-down lists in Kivy
Detailed explanation of Q-learning examples of reinforcement learning
Test shift right: Elk practice of online quality monitoring
信息熵的基础
【第29天】给定一个整数,请你求出它的因子数
[C language] detailed explanation of pointer and array written test questions