当前位置:网站首页>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;
}
边栏推荐
- wirehark数据分析与取证A.pacapng
- uniapp组件-uni-notice-bar通告栏
- 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
- 18_ The wechat video number of wechat applet scrolls and automatically plays the video effect to achieve 2.0
- 【系统分析师之路】第五章 复盘软件工程(开发模型开发方法)
- 力扣 204. 计数质数
- MySQL foundation 04 MySQL architecture
- [fh-gfsk] fh-gfsk signal analysis and blind demodulation research
- Compare version number
- [Cao gongzatan] after working in goose factory for a year in 2021, some of my insights
猜你喜欢
leetcode:871. 最低加油次数【以前pat做过 + 最大堆 +贪心】
看完这篇 教你玩转渗透测试靶机Vulnhub——DriftingBlues-9
给你一个可能存在 重复 元素值的数组 numbers ,它原来是一个升序排列的数组,并按上述情形进行了一次旋转。请返回旋转数组的最小元素。【剑指Offer】
Arduino dy-sv17f automatic voice broadcast
[principles of multithreading and high concurrency: 2. Solutions to cache consistency]
[androd] module dependency replacement of gradle's usage skills
Using tensorboard to visualize the model, data and training process
MySQL --- 数据库查询 - 条件查询
MySQL基础用法02
Androd gradle's substitution of its use module dependency
随机推荐
leetcode:871. 最低加油次数【以前pat做过 + 最大堆 +贪心】
wirehark数据分析与取证A.pacapng
[day 29] given an integer, please find its factor number
leetcode 6103 — 从树中删除边的最小分数
Database SQL language 02 connection query
How is the mask effect achieved in the LPL ban/pick selection stage?
[fh-gfsk] fh-gfsk signal analysis and blind demodulation research
Look at how clothing enterprises take advantage of the epidemic
数学知识:Nim游戏—博弈论
1696C. Fishingprince plays with array [thinking questions + intermediate state + optimized storage]
Now that the teenager has returned, the world's fireworks are the most soothing and ordinary people return to work~
Matlab Doppler effect produces vibration signal and processing
Button wizard play strange learning - go back to the city to buy medicine and add blood
Specified interval inversion in the linked list
【第29天】给定一个整数,请你求出它的因子数
Assets, vulnerabilities, threats and events of the four elements of safe operation
Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
Dotconnect for PostgreSQL data provider
[principles of multithreading and high concurrency: 2. Solutions to cache consistency]
tail -f 、tail -F、tailf的区别