当前位置:网站首页>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;
}
边栏推荐
- 按键精灵打怪学习-多线程后台坐标识别
- 数学知识:台阶-Nim游戏—博弈论
- Thinkphp+redis realizes simple lottery
- 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
- [untitled]
- 【我的OpenGL学习进阶之旅】关于欧拉角、旋转顺序、旋转矩阵、四元数等知识的整理
- 如今少年已归来,人间烟火气最抚凡人心 复工了~
- C#应用程序界面开发基础——窗体控制(3)——文件类控件
- 2022 Jiangxi Provincial Safety Officer B certificate reexamination examination and Jiangxi Provincial Safety Officer B certificate simulation examination question bank
- 12_ Implementation of rolling automatic video playback effect of wechat video number of wechat applet
猜你喜欢
![[untitled]](/img/fd/f6b90536f10325a6fdeb68dc49c72d.png)
[untitled]

Assets, vulnerabilities, threats and events of the four elements of safe operation

异步、邮件、定时三大任务

Database SQL language 01 where condition

强化学习 Q-learning 实例详解

Correctly distinguish the similarities and differences among API, rest API, restful API and web service

MySQL - database query - condition query

leetcode 2097 — 合法重新排列数对

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

Detailed explanation of Q-learning examples of reinforcement learning
随机推荐
Embrace the safety concept of platform delivery
如今少年已归来,人间烟火气最抚凡人心 复工了~
MySQL
[flutter] icons component (fluttericon Download Icon | customize SVG icon to generate TTF font file | use the downloaded TTF icon file)
寻找标杆战友 | 百万级实时数据平台,终身免费使用
JS inheritance and prototype chain
Expérience de recherche d'emploi d'un programmeur difficile
一位苦逼程序员的找工作经历
1696C. Fishingprince Plays With Array【思维题 + 中间状态 + 优化存储】
How wide does the dual inline for bread board need?
leetcode:871. 最低加油次数【以前pat做过 + 最大堆 +贪心】
tail -f 、tail -F、tailf的区别
Delete duplicate elements in the ordered linked list -ii
【FH-GFSK】FH-GFSK信号分析与盲解调研究
1696C. Fishingprince plays with array [thinking questions + intermediate state + optimized storage]
Top ten regular spot trading platforms 2022
The latest analysis of tool fitter (technician) in 2022 and the test questions and analysis of tool fitter (technician)
【C语言】指针与数组笔试题详解
Leetcode 2097 - Legal rearrangement of pairs
产业互联网的产业范畴足够大 消费互联网时代仅是一个局限在互联网行业的存在