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. SyntaxHighlighter -- 代码高亮插件

    SyntaxHighlighter 下载文件里面支持皮肤匹配. 地址:http://alexgorbatchev.com/SyntaxHighlighter/

  2. “main cannot be resolved or is not a field”解决方案

    .layout.main总是在layout上有错误提示波浪线. 解决方法: (1) 删除"import android.R;". (2) 勾选上Eclipse中的"Pro ...

  3. codeforces 334A - Candy Bags

    忘了是偶数了,在纸上画奇数画了半天... #include<cstdio> #include<cstring> #include<cstdlib> #include ...

  4. 【转】个人常用iOS第三方库以及XCode插件介绍 -- 不错

    原文网址:http://adad184.com/2015/07/08/my-favorite-libraries-and-plugins/ 第三方库是现在的程序员离不开的东西 不光是APP开发 基本上 ...

  5. z-index的妙用

    总是在纠结一个问题,当然我是前端初学者.这个问题就是,一个元素放在另一个元素里面,总希望它显示时,但是别撑开元素.这个时候一定要想到z-index. 例如今天写的一个浮动在导航栏下面的一个图片,我用的 ...

  6. DevExpress z

    1.TextEditor(barEditItem)取文本 string editValue = barEditItem1.EditValue.ToString();    //错误,返回null st ...

  7. Linux 系统编程

    简介和主要概念 Linux 系统编程最突出的特点是要求系统程序员对它们工作的的系统的硬件和操作系统有深入和全面的了解,当然它们还有库和系统调用上的区别. 系统编程分为:驱动编程.用户空间编程和网络编程 ...

  8. [Irving]WPF Invalid character in the given encoding. Line xx, position xx.' XML is not valid.

    WPF开发中发现Xaml界面中突然抽风似的提示错误 Invalid character in the given encoding. Line xx, position xx.' XML is not ...

  9. 如何解决Rally模板提示angular js加载错误

    [前言] Rally是一个开源测试工具,用于测试openstack各个组件的性能 在使用Rally测试完毕后,一般会生成测试报告,这点很重要.但是原生态的Rally报告模板angular js框架是从 ...

  10. 《Python 学习手册4th》 第十五章 文档

    ''' 时间: 9月5日 - 9月30日 要求: 1. 书本内容总结归纳,整理在博客园笔记上传 2. 完成所有课后习题 注:“#” 后加的是备注内容 (每天看42页内容,可以保证月底看完此书) “重点 ...