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. sql 查询所有数据库、表名、表字段总结

    ms sql server 1.查询所有表select [id], [name] from [sysobjects] where [type] = 'u' order by [name]2.查询所有数 ...

  2. [LeetCode]题解(python):106-Construct Binary Tree from Inorder and Postorder Traversal

    题目来源: https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意分析 ...

  3. PhoneGap笔记-01 基本使用

    1. 环境配置 1.1 常用框架 jQuery Backbone.js dojo bootstrap kendo UI Sencha jQuery Mobile PhoneJS AngularJS I ...

  4. MVC-03 控制器(5)

    八.动作过滤器 有时在运行Action之前或之后会需要运行一些逻辑运算,以及处理一些运行过程中所生成的异常状况,为了满足这个需求,ASP.NET MVC提供动作过滤器(Action Filter)来处 ...

  5. cocos2d-x中的尺寸之一

    cocos2d-x中的尺寸函数繁多,官方文档对各种尺寸没有很好的描述,网上文章更加寥寥,对尺寸和位置的理解如果不到位,写代码就非常困难,需要猜测尝试,效率低下.这个文章我将研究一些我所迷惑的尺寸函数, ...

  6. kinect for windows - DepthBasics-D2D详解之三

    这篇文章我们将总结一下,之前两篇文章中提到的Kinect SDK的函数接. 函数接口: NuiGetSensorCount: 获取连接的Kinect设备个数 原型:_Check_return_ HRE ...

  7. 宣布发布 Windows Azure SDK 2.2,正式发布 Windows Azure Backup 和 Hyper-V 恢复管理器预览版

    开发人员正逐渐转向云计算,因为它具有众多优势,包括成本.自动化和让开发人员专注于应用程序逻辑的能力.我们很高兴地宣布,继推出 Visual Studio 2013 之后,今天将发布 Windows A ...

  8. BZOJ 1192 鬼谷子的钱袋 (二进制思想)

    题解:鉴于二进制的思想来划分 #include <cstdio> int main(){ int n,d=0;scanf("%d",&n); while(1&l ...

  9. Ubuntu 12.10 安装JDK7

    1.首先到oracle下载上下载jdk-7u25-linux-i586.tar.gz 2.将jdk-7u25-linux-i586.tar.gz复制到/usr/lib/jvm/目录以下,这里假设没有j ...

  10. BZOJ 2190 仪仗队

           这道题的关键就是找到所有的点中,x与y互质的点,第一反应必定是暴搜,稍想一下可以从中分开求一半,但范围仍然限定了这条路行不通,仔细画了几张图后发现下图中从第三行起第k行可连fai k个, ...