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. bzoj2431: [HAOI2009]逆序对数列

    dp. f[i][j]表示放置第i个数有j个逆序对的方案数. s[i][j]维护前缀和(f[i][0]~f[i][j]). 状态转移方程 f[i][j]=s[i-1][j]-s[i-1][max(j- ...

  2. 运维角度浅谈MySQL数据库优化(转)

    一个成熟的数据库架构并不是一开始设计就具备高可用.高伸缩等特性的,它是随着用户量的增加,基础架构才逐渐完善.这篇博文主要谈MySQL数据库发展周期中所面临的问题及优化方案,暂且抛开前端应用不说,大致分 ...

  3. #include<unistd.h>头文件的理解

    1.百度百科定义 unistd.h 是 C 和 C++ 程序设计语言中提供对 POSIX 操作系统 API 的访问功能的头文件的名称.该头文件由 POSIX.1 标准(单一UNIX规范的基础)提出,故 ...

  4. Android 实现emoji表情的demo

    Android 实现emoji表情的例子,网上看到的,记录一下. 请看下图 : 项目下载地址:http://download.csdn.net/detail/abc13939746593/741397 ...

  5. sharepoint SPFolder的使用

    转:http://blog.csdn.net/pclzr/article/details/7591731 SPFolder是SharePoint对象模型中文件夹相关的类,它的使用方法相对比较简单.获取 ...

  6. IOS NSNotificationCenter 通知的使用

    1.注册通知 [NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notify) name:@" ...

  7. Multiple View Geometry in Computer Vision Second Edition by Richard Hartley 读书笔记(二)

    // Chapter 2介绍的是2d下的投影变换,摘录下了以下定理 Result 2.1. The point x lies on the line l if and only if xTl = 0. ...

  8. 解决YUM无法正常工作

    1. 错误发生背景 在进行安装依赖包的时候,能够在YUM源中找到相关的RPM包,但是无法进行下载,在单独进行安装RPM包的时候能够进行安装,报错截图如下: 具体的报错信息如下: Error Downl ...

  9. [转]天龙八部服务器端Lua脚本系统

    一.Lua脚本功能接口 1. LuaInterface.h/.cpp声明和实现LuaInterface. LuaInterface成员如下: //脚本引擎 FoxLuaScriptmLua ; //注 ...

  10. Android JNI之JAVA与C++对象建立对称关联(JNI优化设计,确保JNI调用的稳定性)

    转载请声明:原文转自:http://www.cnblogs.com/xiezie/p/5930503.html Android JNI之JAVA与C++对象建立对称关联 1.JAVA对象持有C++对象 ...