当前位置:网站首页>容斥原理 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);
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
猜你喜欢
QQ电脑版取消转义符输入表情
Simple selection sort of selection sort
Single chip computer engineering experience - layered idea
1.13 - RISC/CISC
NotImplementedError: Cannot convert a symbolic Tensor (yolo_boxes_0/meshgrid/Size_1:0) to a numpy ar
Spark中groupByKey() 和 reduceByKey() 和combineByKey()
Overview of variable resistors - structure, operation and different applications
MySQL advanced part 2: the use of indexes
Sqlmap tutorial (II) practical skills I
Operator priority, one catch, no doubt
随机推荐
【Rust 笔记】13-迭代器(中)
Leetcode-22: bracket generation
Overview of variable resistors - structure, operation and different applications
Records of some tools 2022
Matrixdb V4.5.0 was launched with a new mars2 storage engine!
1040 Longest Symmetric String
MySQL advanced part 1: stored procedures and functions
LeetCode 0108.将有序数组转换为二叉搜索树 - 数组中值为根,中值左右分别为左右子树
leetcode-31:下一个排列
Leetcode divide and conquer / dichotomy
How to understand the definition of sequence limit?
Error ora-28547 or ora-03135 when Navicat connects to Oracle Database
leetcode-6110:网格图中递增路径的数目
【Rust 笔记】17-并发(上)
Leetcode-6111: spiral matrix IV
Leetcode backtracking method
How to generate an image from text on fly at runtime
1041 Be Unique
1.13 - RISC/CISC
【LeetCode】Day95-有效的数独&矩阵置零