How many integers can you find

Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

 
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和M 输入M个数,找出所有M个数的倍数并且,Mi的倍数小于N,输出所有数的总个数。 如果一个数同时是三个数的倍数
单独记一个数的倍数次数为C(3,1) =3
记两个数的倍数次数为 C(3,2)=3
记三个数的倍数次数为 C(3,3)=1
3-3+1=1,只记一次依次类推 一个数为5个数的倍数
C(5,1)=5
C(5,2)=10
C(5,3)=10
C(5,4)=5
C(5,5)=1
5-10+10-5+1=1 六个数
C(6,1)=6
C(6,2)=15
C(6,3)=20
C(6,4)=15
C(6,5)=6
C(6,6)=1
6-15+20-15+6-1=1

上图:

然后因为数字不超过10个,可以运用枚举子集的思想去做这个题目。
所以用到DFS。
最后有一个地方要注意就是在DFS里面判断积这里,要用GCD,一开始没想到过不了样例。 AC代码:
/*/
#include"map"
#include"cmath"
#include"string"
#include"cstdio"
#include"vector"
#include"cstring"
#include"iostream"
#include"algorithm"
using namespace std;
typedef long long LL; LL a[15];
int n,m,cnt;
LL ans,x; LL gcd(LL a,LL b){
return b?gcd(b,a%b):a;
} void DFS(int x,LL axb,int num) {
axb=a[x]/gcd(a[x],axb)*axb;
if(num&1) ans+=(n-1)/axb;
else ans-=(n-1)/axb;
// cout<<"now ans is:"<<ans<<endl; //检查
for(int i=x+1; i<cnt; i++)
DFS(i,axb,num+1);
} int main() {
while(~scanf("%d%d",&n,&m)) {
ans=0;
cnt=0;
for(int i=0; i<m; i++) {
scanf("%I64d",&x);
if(x!=0)a[cnt++]=x;
}
for(int i=0; i<cnt; i++){
DFS(i,a[i],1); //用DFS去枚举每种选择的情况。
}
printf("%d\n",ans);
}
return 0;
}

  

ACM: How many integers can you find-数论专题-容斥原理的简单应用+GCD的更多相关文章

  1. 【数论Day1】 最大公约数(gcd)题目

    20170529-3数论_gcd 题解: http://www.cnblogs.com/ljc20020730/p/6919116.html 日期 序号 题目名称 输入文件名 输出文件名 时限 内存 ...

  2. ACM: POJ 1061 青蛙的约会 -数论专题-扩展欧几里德

    POJ 1061 青蛙的约会 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu  Descr ...

  3. ACM学习历程—HDU 5446 Unknown Treasure(数论)(2015长春网赛1010题)

    Problem Description On the way to the next secret treasure hiding place, the mathematician discovere ...

  4. ACM学习历程—HDU 5317 RGCDQ (数论)

    Problem Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more an ...

  5. ACM数论之旅3---最大公约数gcd和最小公倍数lcm(苦海无边,回头是岸( ̄∀ ̄))

    gcd(a, b),就是求a和b的最大公约数 lcm(a, b),就是求a和b的最小公倍数 然后有个公式 a*b = gcd * lcm     ( gcd就是gcd(a, b), ( •̀∀•́ ) ...

  6. ACM学习历程—BZOJ2956 模积和(数论)

    Description 求∑∑((n mod i)*(m mod j))其中1<=i<=n,1<=j<=m,i≠j. Input 第一行两个数n,m. Output 一个整数表 ...

  7. 数论(容斥原理)hdu-4509-The Boss on Mars

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4059 题目大意: 给一个n,求1~n中与n互质的数的4次方的总和. 解题思路: 容斥原理.逆元.公式 ...

  8. 邝斌带你飞之数论专题--Maximum GCD UVA - 11827

    Given the N integers, you have to find the maximum GCD (greatest common divisor) of every possible p ...

  9. NOIP2018提高组金牌训练营——数论专题

    地址 https://www.51nod.com/live/liveDescription.html#!liveId=23 1187 寻找分数 给出 a,b,c,d, 找一个分数p/q,使得a/b & ...

随机推荐

  1. Pyqt 以OOP方式动画的效果改变自身窗体大小

    代码: # -*- coding:utf8 -*- from PyQt4.QtGui import * from PyQt4.QtCore import * import sys class ani( ...

  2. EF环境搭建碰到的问题

    研究EF Code Frist安装Entity Framework的时候,遇到了一些问题,下面就描述一下这些问题,顺便附上问题的解决办法. 1.Nuget安装EF的时候,一直报错,解决的办法是,卸载N ...

  3. poj 2524:Ubiquitous Religions(并查集,入门题)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23997   Accepted:  ...

  4. 第二十三篇:在SOUI中使用LUA脚本开发界面

    像写网页一样做客户端界面可能是很多客户端开发的理想. 做好一个可以实现和用户交互的动态网页应该包含两个部分:使用html做网页的布局,使用脚本如vbscript,javascript做用户交互的逻辑. ...

  5. 人性的弱点&&影响力

    How wo win friends and influence people 人性的弱点 by 卡耐基 人际关系基本技巧 不要批评.谴责.抱怨 真诚的欣赏他人 激发他人的渴望 获得别人好感的方式 微 ...

  6. Arduino101学习(一)——Windows下环境配置

    一.Arduino IDE下载 要开发Arduino 101/Genuino 101,你需要先安装并配置好相应的开发环境.下载地址 http://www.arduino.cn/thread-5838- ...

  7. 智能车学习(二十)——浅谈C车硬连接与软连接

    一.为何要追求软连接?       车子进行软连接之后,可以达到一种效果,就是在高速过程中,车子如果快要发生侧翻的时候,只会跳一个后轮,且只是轻微,而前轮如果进行的内倾,就可以让前轮最大面积接触,增大 ...

  8. Jmeter 分布式性能测试

    作为一个纯 JAVA 的GUI应用,JMeter 对于CPU和内存的消耗还是很惊人的,所以当需要模拟数以千计的并发用户时,使用单台机器模拟所有的并发用户就有些力不从心,甚至还会引起JAVA内存溢出的错 ...

  9. 解决android expandablelistview 里面嵌入gridview行数据重复问题

    最近做了一个“csdn专家博客App” 当然了是android版本,在专家浏览页面,我才用了expandablelistview 组件来显示专家分类,每个分类点击之后可以显示专家的头像和名字. 很简单 ...

  10. HDU 5652 India and China Origins(经典并查集)

    特别经典的一个题,还有一种方法就是二分+bfs 题意:空间内n*m个点,每个点是0或者1,0代表此点可以走,1代表不能走.接着经过q年,每年一个坐标表示此点不能走.问哪年开始图上不能出现最上边不能到达 ...