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. <精华篇>:iOS视频大全-持续更新

    注意:新浪微博分享的资料和简书分享的资料,略有不同! 小码哥swift3.0版 斗鱼项目视频:点击下载  iOS开发25个项目实战:点击下载 2016PHP全套下载:点击下载  黑马刀哥iOS视频精选 ...

  2. HDOJ----------1009

    题目: FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  3. cmd命令添加一个应用程序到防火墙例外项中

    windows+r 键打开运行 输入cmd按回车 把下列命令在cmd中点击右键粘贴进去,按回车,即可添加到防火墙例外程序列表中. netsh advfirewall firewall delete r ...

  4. JAVA中把ResultSet转换成LIST

    项目中老是遇到数据库异常关闭的情况,真烦, 想用hibernate呢,那个玩意儿又太笨重,感慨C#和PHP的舒适方便性,模拟TP写了个数据处理层,将就用着先代码里有很多项目中的东西,不要直接COPY了 ...

  5. 模拟美萍加密狗--Rockey2虚拟狗(四)

    目录(?)[+]       首先,抱怨一下.学校个破网,似乎把我端口封了,死活分不上IP,也许是是我MAC改的太频繁了,有盗号嫌疑…… 然后,正文开始…… 其实虚拟狗几天前就写完了,可这几天上不了网 ...

  6. 试用阿里云RDS的MySQL压缩存储引擎TokuDB

    以前就用过自己搭建MySQL服务器的两种存储引擎MyISAM和InnoDB(也用过一点Memory方式),在今年初转向阿里云关系型数据库服务RDS的时候,看到可调参数中有一个TokuDB,不过不太了解 ...

  7. HDU 3589 Jacobi symbol

    彻底对数学绝望了 #include <cstdio> #include <cmath> int flag[1005],p[500],a; int d[100]; int ini ...

  8. hdu5188 加限制的01背包问题

    http://acm.hdu.edu.cn/showproblem.php? pid=5188 Problem Description As one of the most powerful brus ...

  9. ASP.NET中IsPostBack的理解

    IsPostBack是Page类的一个属性,返回值为一个布尔值. 一般放在Page_Load事件中, 是指是否第一次调用这个页面.当页面是第一次打开时其值为False,若当前页面为一个提交后的页面其值 ...

  10. Visual Studio Tools for Unity安装及使用

    Visual Studio Tools for Unity安装及使用 转载自:CSDN 晃了一下,10.1到现在又过去两个月了,这两个月什么也没有学,整天上班下班,从这周末开始拾起unity,为了年后 ...