How many integers can you find

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

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
 
Author
wangye
 
Source
 


题目大意:很简单的题目,直接看意思就懂哈!


      解题思路:容斥定理,加奇减偶,开始忘记求lcm了,囧!!而且开始还特判0的情况,题目中说的必须是除以,所以0不是一个解。。。开始竟然以为需要是因子就可以了。想通了之后直接先筛选一次,把0都筛选出去。

      题目地址:How many integers can you find

AC代码:
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
using namespace std;
__int64 sum;
int n,m;
int a[25];
int b[25];
int visi[25]; __int64 gcd(__int64 m,__int64 n)
{
__int64 tmp;
while(n)
{
tmp=m%n;
m=n;
n=tmp;
}
return m;
} __int64 lcm(__int64 m,__int64 n)
{
return m/gcd(m,n)*n;
} void cal()
{
int flag=0,i;
__int64 t=1;
__int64 ans;
for(i=0;i<m;i++)
{
if(visi[i])
{
flag++; //记录用了多少个数
t=lcm(t,b[i]);
}
}
ans=n/t;
if(n%t==0) ans--;
if(flag&1) sum+=ans; //加奇减偶
else sum-=ans;
} int main()
{
int i,j,p;
while(~scanf("%d%d",&n,&m))
{
sum=0;
for(i=0;i<m;i++)
scanf("%d",&a[i]); int tt=0; //
for(i=0;i<m;i++)
{
if(a[i]) //去掉0
b[tt++]=a[i];
}
m=tt;
p=1<<m; //p表示选取多少个数,组合数的状态
for(i=1;i<p;i++)
{
int tmp=i;
for(j=0;j<m;j++)
{
visi[j]=tmp&1;
tmp>>=1;
}
cal();
}
printf("%I64d\n",sum);
}
return 0;
} /*
12 2
2 3
12 3
2 3 0
12 4
2 3 2 0
*/ //968MS


HDU 1796How many integers can you find(简单容斥定理)的更多相关文章

  1. HDU1796 How many integers can you find【容斥定理】

    题目链接: http://acm.hdu.edu.cn/showproblem.php? pid=1796 题目大意: 给你一个整数N.和M个整数的集合{A1.A2.-.Am}.集合内元素为非负数(包 ...

  2. HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  3. 牛客练习赛43-F(简单容斥)

    题目链接:https://ac.nowcoder.com/acm/contest/548/F 题意:简化题意之后就是求[1,n]中不能被[2,m]中的数整除的数的个数. 思路:简单容斥题,求[1,n] ...

  4. hdu 4135 [a,b]中n互质数个数+容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=4135 给定一个数n,求某个区间[a,b]内有多少数与这个数互质. 对于一个给定的区间,我们如果能够求出这个区间内 ...

  5. HDU 4135 Co-prime 欧拉+容斥定理

    Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  6. HDU 1695 GCD(容斥定理)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  7. 题解报告:hdu 4135 Co-prime(容斥定理入门)

    Problem Description Given a number N, you are asked to count the number of integers between A and B ...

  8. HDU 2841 Visible Trees(容斥定理)

    Visible Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  9. luogu P6583 回首过去 简单数论变换 简单容斥

    LINK:回首过去 考试的时候没推出来 原因:状态真的很差 以及 数论方面的 我甚至连除数分块都给忘了. 手玩几个数据 可以发现 \(\frac{x}{y}\)满足题目中的条件当且仅当 这个是一个既约 ...

随机推荐

  1. 如何使用UIAutomation进行iOS 自动化测试(Part I)

    转自:http://www.cnblogs.com/vowei/archive/2012/08/10/2631949.html 写在前面 研究iOS的自动化测试也有些日子了,刚开始的时候,一直苦于找不 ...

  2. bzoj2875: [Noi2012]随机数生成器

    矩阵乘法. x[n] = {x[0],1} * ( {a,0} ^ n ) {b,1} 写成这样谁能看懂.... noi里的大水题.我居然 #include<cstdio> #includ ...

  3. CodeForces Round #296 Div.2

    A. Playing with Paper 如果a是b的整数倍,那么将得到a/b个正方形,否则的话还会另外得到一个(b, a%b)的长方形. 时间复杂度和欧几里得算法一样. #include < ...

  4. 【C#学习笔记】获取当前应用程序所在路径及环境变量

    转自:http://www.cnblogs.com/netlyf/archive/2011/06/22/2086718.html 一.获取当前文件的路径 string str1=Process.Get ...

  5. handler.post 为什么要将thread对象post到handler中执行呢?

    转载网址:http://www.cnblogs.com/crazypebble/archive/2011/03/23/1991829.html在Android中使用Handler和Thread线程执行 ...

  6. MOSS 2010:Visual Studio 2010开发体验(14)——列表开发之事件接收器

    转:http://boke.25k5.com/kan141919.html 通过前面几篇,我们已经完成了内容类型,列表定义,列表实例g 8h"@的开发.本篇继续讲解列表中的一个重要环节- ...

  7. POJ 1423 Big Number

    题意:求n阶乘的位数. 解法:斯特林公式,,然后取log10就是位数了,因为精度问题需要化简这个式子,特判1. 代码: #include<stdio.h> #include<iost ...

  8. 【LeetCode 215】Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  9. mapreduce优化总结

    集群的优化 1.合理分配map和reduce任务的数量(单个节点上map任务.reduce任务的最大数量) 2.其他配置 io.file.buffer.size hadoop访问文件的IO操作都需要通 ...

  10. cannot conform to protocol 的一点事

    学习UITableView过程中,回想视频打一遍代码,发现卡在了第一步.一直显示无法继承协议,而且还多了一个错误:definition conflicts with previous value.百度 ...