当前位置:网站首页>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;
}
边栏推荐
- 12_ Implementation of rolling automatic video playback effect of wechat video number of wechat applet
- Basic concept and implementation of overcoming hash
- Merge K sorted linked lists
- Excel calculates the difference between time and date and converts it into minutes
- Androd Gradle 对其使用模块依赖的替换
- MySQL基础用法02
- Trois tâches principales: asynchrone, courrier et timing
- leetcode 6103 — 从树中删除边的最小分数
- SwiftUI 组件大全之使用 SceneKit 和 SwiftUI 构建交互式 3D 饼图(教程含源码)
- tail -f 、tail -F、tailf的区别
猜你喜欢
看完这篇 教你玩转渗透测试靶机Vulnhub——DriftingBlues-9
dotConnect for PostgreSQL数据提供程序
Meituan dynamic thread pool practice ideas, open source
Basic concept and implementation of overcoming hash
leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]
攻克哈希的基本概念与实现
Cut point of undirected graph
MySQL
[principles of multithreading and high concurrency: 2. Solutions to cache consistency]
Niu Ke swipes questions and clocks in
随机推荐
[flutter] icons component (fluttericon Download Icon | customize SVG icon to generate TTF font file | use the downloaded TTF icon file)
MySQL foundation 05 DML language
按键精灵打怪学习-回城买药加血
寻找标杆战友 | 百万级实时数据平台,终身免费使用
Tp6 fast installation uses mongodb to add, delete, modify and check
Kivy教程大全之如何在 Kivy 中创建下拉列表
The industrial scope of industrial Internet is large enough. The era of consumer Internet is only a limited existence in the Internet industry
Find a benchmark comrade in arms | a million level real-time data platform, which can be used for free for life
Arduino dy-sv17f automatic voice broadcast
[androd] module dependency replacement of gradle's usage skills
一位苦逼程序员的找工作经历
Daily topic: movement of haystack
2022 Jiangxi Provincial Safety Officer B certificate reexamination examination and Jiangxi Provincial Safety Officer B certificate simulation examination question bank
Machine learning terminology
SwiftUI 组件大全之使用 SceneKit 和 SwiftUI 构建交互式 3D 饼图(教程含源码)
wirehark数据分析与取证A.pacapng
【无标题】
Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
2022 cable crane driver examination registration and cable crane driver certificate examination
[self management] time, energy and habit management