How many integers can you find

Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6001    Accepted Submission(s): 1722

Problem Description
  Now you get a number N, and a M-integers set, you should find out how many integers which are small than N, that they can divided exactly by any integers in the set. For example, N=12, and M-integer set is {2,3}, so there is another set {2,3,4,6,8,9,10}, all the integers of the set can be divided exactly by 2 or 3. As a result, you just output the number 7.
 
Input
  There are a lot of cases. For each case, the first line contains two integers N and M. The follow line contains the M integers, and all of them are different from each other. 0<N<2^31,0<M<=10, and the M integer are non-negative and won’t exceed 20.
 
Output
  For each case, output the number.
 
Sample Input
12 2 2 3
 
Sample Output
7
 

题解:题意就是找N-1中与M个数不互斥数的个数,由于4和6,12就可以除4和6,所以要找被选的数的最小公倍数;由于刚开始没考虑这点,直接找了24;注意M个数中可能有0;dfs+容斥,竟然运行时间还短点...

容斥代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
typedef long long LL;
vector<LL>p;
LL gcd(LL a,LL b){return b==0?a:gcd(b,a%b);}
void rc(LL x){
LL sum=0;
for(int i=1;i<(1<<p.size());i++){
LL num=0,cur=1;
for(int j=0;j<p.size();j++){
if(i&(1<<j)){
num++;
cur=cur*p[j]/gcd(cur,p[j]);
}
}//printf("%lld\n",cur);
if(num&1)sum+=x/cur;
else sum-=x/cur;
}
printf("%lld\n",sum);
}
int main(){
LL N,M;
LL x;
// printf("%lld\n",(LL)pow(19,10));
while(~scanf("%lld%lld",&N,&M)){
p.clear();
for(int i=0;i<M;i++){
scanf("%lld",&x);
if(x==0)continue;
p.push_back(x);
}
rc(N-1);
}
return 0;
}

  dfs+容斥:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
typedef long long LL;
LL m[15],ans;
LL N,M;
int k;
LL gcd(LL a,LL b){return b==0?a:gcd(b,a%b);}
void dfs(LL cur,LL pos,int t){
if(t==0){
if(k&1)ans+=N/cur;
else ans-=N/cur;
return;
}
if(pos>M)return;
dfs(cur*m[pos]/gcd(cur,m[pos]),pos+1,t-1);
dfs(cur,pos+1,t);
}
int main(){
LL x;
// printf("%lld\n",(LL)pow(19,10));
while(~scanf("%lld%lld",&N,&M)){
N--;
for(int i=1;i<=M;i++){
scanf("%lld",&x);
if(x==0)continue;
m[i]=x;
}
ans=0;
for(k=1;k<=M;k++){
dfs(1,1,k);
}
printf("%lld\n",ans);
}
return 0;
}

  

How many integers can you find(容斥+dfs容斥)的更多相关文章

  1. HDOJ1796 How many integers can you find(dfs+容斥)

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  2. BZOJ 4671 异或图 | 线性基 容斥 DFS

    题面 Description 定义两个结点数相同的图 G1 与图 G2 的异或为一个新的图 G, 其中如果 (u, v) 在 G1 与 G2 中的出现次数之和为 1, 那么边 (u, v) 在 G 中 ...

  3. 2019.01.17 bzoj1853: [Scoi2010]幸运数字(容斥+dfs)

    传送门 搜索菜题,然而第一次没有注意然后爆longlonglong longlonglong了. 题意:称所有数位由6,86,86,8组成的数为幸运数字,问一个一个区间[l,r][l,r][l,r]中 ...

  4. 15ecjtu校赛1006 (dfs容斥)

    Problem Description 在平面上有一个n*n的网格,即有n条平行于x轴的直线和n条平行于y轴的直线,形 成了n*n个交点(a,b)(1<=a<=n,1<=b<= ...

  5. 【BZOJ1853/2393】[Scoi2010]幸运数字/Cirno的完美算数教室 DFS+容斥

    [BZOJ1853][Scoi2010]幸运数字 Description 在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认为,于是他定义自己的“幸运号码”是十进制表示中只包含数字6和8的那 ...

  6. bzoj 2839 集合计数 容斥\广义容斥

    LINK:集合计数 容斥简单题 却引出我对广义容斥的深思. 一直以来我都不理解广义容斥是为什么 在什么情况下使用. 给一张图: 这张图想要表达的意思就是这道题目的意思 而求的东西也和题目一致. 特点: ...

  7. Eddy's爱好(dfs+容斥)

    Eddy's爱好 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  8. 广州工业大学2016校赛 F 我是好人4 dfs+容斥

    Problem F: 我是好人4 Description 众所周知,我是好人!所以不会出太难的题,题意很简单 给你n个数,问你1000000000(含1e9)以内有多少个正整数不是这n个数任意一个的倍 ...

  9. P2567 [SCOI2010]幸运数字 DFS+容斥定理

    P2567 [SCOI2010]幸运数字 题目描述 在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认为,于是他定义自己的“幸运号码”是十进制表示中只包含数字6和8的那些号码,比如68,66 ...

随机推荐

  1. BootStrap 智能表单系列 五 表单依赖插件处理

    这一章比较简单哦,主要就是生产表单元素后的一些后续处理操作,比如日期插件的渲染.一些autocomplete的处理等,在回调里面处理就可以了, demo: $("input.date-pic ...

  2. Matlab常用小技巧及部分快捷键

    Matlab常用小技巧一: 1. m文件如果是函数,保存的文件名最好与函数名一致,这点都很清楚.不过容易疏忽的是,m文件名的命名尽量不要是简单的英文单词,最好是由大小写英文/数字/下划线等组成.原因是 ...

  3. IOS使用pch预编译文件

    首先新建一个pch文件,然后要修改这个项目的Build Setting中的Prefix Header 修改为 $(SRCROOT)/项目名称/预编译文件名: 一般pch文件的用处: 1.导入框架,如: ...

  4. (Problem 41)Pandigital prime

    We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly o ...

  5. 细说SSO单点登录(转)

    什么是SSO? 如果你已知道,请略过本节! SSO核心意义就一句话:一处登录,处处登录:一处注销,处处注销.即:在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统. 很多人容易把SS ...

  6. Java算法——O(n)查询数列中出现超过半数的元素

    主要思想: 相邻元素两两比较,如果相同存入新数组,不同二者都删除.如果 某数出现次数超高n/2,则最后剩下的1元素为所求. public static int findMostElem(final A ...

  7. hdoj 2896 病毒侵袭(AC自动机)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2896 思路分析:题目为模式匹配问题,对于一个给定的字符串,判断能匹配多少个模式:该问题需要静态建树,另 ...

  8. 带你轻松玩转Git--图解三区结构

    在上篇文章的结尾我们提到了Git 的三区结构,在版本控制体系中有这样两种体系结构,一种是两区结构一种是三区结构.接下来我们通过对Git三区的结构学习来帮助我们更好的去理解并运用Git. 两区结构是其他 ...

  9. 用javap命令反编译来分析字符串问题

    编写Test.java.编译完后生成Test.class文件,然后对该文件运行javap -c Test命令,生成字节码指令.阅读并得出结论 一.s1和s2指向常量池的不同常量 ①java代码 pub ...

  10. css优先级及权重值

    优先级: 外部样式表 内部样式表(位于<head>标签内部 内联样式(在HTML元素内部)优先权最高 内联样式>内部样式=外部样式(看具体引入位置,解析的先后) 权重值: 第一等:内 ...