当前位置:网站首页>容斥原理 AcWing 890. 能被整除的数
容斥原理 AcWing 890. 能被整除的数
2022-07-05 06:16:00 【T_Y_F666】
容斥原理 AcWing 890. 能被整除的数
原题链接
算法标签
容斥原理
思路
代码
#include<bits/stdc++.h>
#define int long long
#define abs fabs
#define rep(i, a, b) for(int i=a;i<b;++i)
#define Rep(i, a, b) for(int i=a;i>=b;--i)
using namespace std;
const int N = 20;
int a[N];
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void put(int x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) put(x/10);
putchar(x%10^48);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n=read(), m=read();
rep(i, 0, m){
a[i]=read();
}
int res=0;
// 枚举从1 到 1111...(m个1)的每一个集合状态, (至少选中一个集合)
rep(i, 1, 1<<m){
// t表示质数乘积 cnt表示当前选法集合数量
int t=1, cnt=0;
rep(j, 0, m){
if(i>>j&1){
// 乘积大于n, 则n/t = 0, 跳出这轮循环
if(t*a[j]>n){
t=-1;
break;
}
cnt++;
t*=a[j];
}
}
if(t!=-1){
if(cnt%2){
res+=n/t;
}else{
res-=n/t;
}
}
}
printf("%lld", res);
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- 数据可视化图表总结(一)
- Leetcode-1200: minimum absolute difference
- Daily question 1189 Maximum number of "balloons"
- MySQL advanced part 2: MySQL architecture
- MySQL怎么运行的系列(八)14张图说明白MySQL事务原子性和undo日志原理
- Shutter web hardware keyboard monitoring
- leetcode-6111:螺旋矩阵 IV
- Appium foundation - use the first demo of appium
- MySQL advanced part 2: the use of indexes
- TypeScript 基础讲解
猜你喜欢
MySQL advanced part 1: index
leetcode-6110:网格图中递增路径的数目
Leetcode stack related
Leetcode-6110: number of incremental paths in the grid graph
快速使用Amazon MemoryDB并构建你专属的Redis内存数据库
可变电阻器概述——结构、工作和不同应用
liunx启动redis
Chapter 6 relational database theory
MySQL advanced part 2: the use of indexes
Data visualization chart summary (I)
随机推荐
TypeScript 基础讲解
对for(var i = 0;i < 5;i++) {setTimeout(() => console.log(i),1000)}的深入分析
【LeetCode】Easy | 20. Valid parentheses
阿里新成员「瓴羊」正式亮相,由阿里副总裁朋新宇带队,集结多个核心部门技术团队
11-gorm-v2-03-basic query
【Rust 笔记】16-输入与输出(下)
SQLMAP使用教程(一)
QQ电脑版取消转义符输入表情
Simple selection sort of selection sort
Leetcode-9: palindromes
11-gorm-v2-02-create data
927. 三等分 模拟
Is it impossible for lamda to wake up?
Leetcode-556: the next larger element III
可变电阻器概述——结构、工作和不同应用
[rust notes] 16 input and output (Part 2)
Sqlmap tutorial (II) practical skills I
Leetcode heap correlation
Doing SQL performance optimization is really eye-catching
Leetcode-31: next spread