How many integers can you find

Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 5556    Accepted Submission(s): 1593

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以内可以被所给的集合中的数整除的数的个数。



解题思路:

这里要运用我们所说的容斥原理。

所谓容斥原理,运用起来要记住“奇加偶减”。

比方求100以内能被2,3,11,13,41整除的数的个数,我们即u(i)为100以内能被i整除的数的个数。

那么答案就是:

u(2)+u(3)+u(11)+u(13)+u(41)

-u(2*3)-u(3*11)-u(11*13)-u(13*41)

+u(2*3*11)+u(3*11*13)+u(11*13*41)

-u(2*3*11*13)-u(3*11*13*41)

+u(2*3*11*13*41)

这就是所谓的“奇加偶减”。

同一时候n以内能被i整除的数的个数为(n-1)/i。

综上。我们就能够通过枚举集合中的数,再容斥来得到答案。

枚举有2中方法:暴力枚举和dfs。因为m最大仅仅有10。暴力枚举时我们能够使用二进制来代表某个状态,每一位代表去与不取。dfs就非常easy了。

參考代码:

/*
二进制
Memory: 1568 KB Time: 639 MS
Language: G++ Result: Accepted
*/
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cctype>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const double eps=1e-10;
const int INF=0x3f3f3f3f;
const int MAXN=25;
typedef long long LL; int n,m,num[MAXN],divi[MAXN]; int gcd(int a,int b)
{
return b?gcd(b,a%b):a;
} int lcm(int a,int b)
{
return a/gcd(a,b)*b;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
while(scanf("%d%d",&n,&m)!=EOF)
{
int cnt=0;
for(int i=0;i<m;i++)
{
scanf("%d",&num[i]);
if(num[i])
divi[cnt++]=num[i];
}
m=cnt;
int ans=0;
for(int k=1;k<(1<<m);k++)
{
int select=0,tlcm=1;
for(int i=0;i<m;i++)
{
if(k&(1<<i))
{
select++;
tlcm=lcm(tlcm,divi[i]);
}
}
if(select&1)
ans+=(n-1)/tlcm;
else
ans-=(n-1)/tlcm;
}
printf("%d\n",ans);
}
return 0;
}
/*
dfs
Memory: 1572 KB Time: 109 MS
Language: G++ Result: Accepted
*/
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cctype>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const double eps=1e-10;
const int INF=0x3f3f3f3f;
const int MAXN=25;
typedef long long LL; int n,m,num[MAXN],divi[MAXN],ans; int gcd(int a,int b)
{
return b? gcd(b,a%b):a;
} int lcm(int a,int b)
{
return a/gcd(a,b)*b;
} void dfs(int pos,int tlcm,int select)
{
//if(pos>m)
// return ;
tlcm=lcm(tlcm,divi[pos]);
select++;
if(select&1)
ans+=(n-1)/tlcm;
else
ans-=(n-1)/tlcm;
for(int i=pos+1;i<m;i++)
dfs(i,tlcm,select);
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
while(scanf("%d%d",&n,&m)!=EOF)
{
int cnt=0;
for(int i=0; i<m; i++)
{
scanf("%d",&num[i]);
if(num[i])
divi[cnt++]=num[i];
}
m=cnt;
ans=0;
for(int i=0;i<m;i++)
dfs(i,1,0);
printf("%d\n",ans);
}
return 0;
}

最新文章

  1. Python的第一天
  2. oracle合并版本
  3. TRUNK的作用功能.什么是TRUNK
  4. python 学习笔记九 队列,异步IO
  5. php错误级别的设置方法
  6. webpack与gulp的区别及实例搭建
  7. 康力优蓝机器人 -- 优友U05类人型机器人发布
  8. HP原装硒鼓
  9. openstack中彻底删除计算节点的操作记录
  10. WebSphere MQ 入门指南
  11. 推荐一个网站Stack Overflow
  12. Part 32 Abstract classes in c#
  13. 关于kali安装vmware的坑,linux套路太深。
  14. 【转】Informix数据表结构分析资料整理之约束查询代码
  15. CSU 1004并查集
  16. [SDOI2011]染色
  17. (CLR-Via-C#) 类型基础
  18. Win8.1开启Hyper-V并设置虚拟机联网
  19. 史上最全office2016 激活码
  20. MacOs brew 命令行安装常见工具

热门文章

  1. Functor、Applicative 和 Monad x
  2. python远程控制电脑
  3. python note of decorator
  4. 诊断:ORA-16188: LOG_ARCHIVE_CONFIG settings inconsistent with previously started instance
  5. org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException 前言中不允许有内容 来自类路径资源的XML文档中的第1行是无效的
  6. ehcache的学习笔记(一)
  7. 零基础入门学习Python(30)--文件系统:介绍一个高大上的东西
  8. 201621123079《Java程序设计》第1周学习总结
  9. JavaScript小技巧整理篇(非常全)
  10. win10永久激活