How many integers can you find

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

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
 
题意:在 1-(n-1) 中能够被输入的数字整除的数字的数量。
思路:容斥原理+枚举状态,碰到奇数加上(n-1)/lcm(a,b,c..) 碰到偶数减(n-1)/lcm(a,b,c...) 注意0不能取,发现本人一直不是很会用深搜,所以还是用状压了 = =
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
int gcd(int a,int b){
return b==?a:gcd(b,a%b);
}
int lcm(int a,int b){
return a/gcd(a,b)*b;
}
int main()
{
int n,m,a[];
while(scanf("%d%d",&n,&m)!=EOF){
int id = ,num;
for(int i=;i<m;i++){
scanf("%d",&num);
if(num!=) a[id++] = num;
}
int ans = ;
for(int i=;i<(<<id);i++){
int l=,cnt=;
for(int j=;j<id;j++){
if((i>>j)&){
cnt++;
l = lcm(l,a[j]);
}
}
if(cnt&){
ans+=(n-)/l; ///不包括自身所以n-1
}else{
ans-=(n-)/l;
}
}
printf("%d\n",ans);
}
return ;
}

hdu 1796(容斥原理+状态压缩)的更多相关文章

  1. hdu 2841(容斥原理+状态压缩)

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

  2. hdu 5094 Maze 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...

  3. hdu 5724 SG+状态压缩

    Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  4. [BZOJ 4455] [ZJOI 2016] 小星星 (树形dp+容斥原理+状态压缩)

    [BZOJ 4455] [ZJOI 2016] 小星星 (树形dp+容斥原理+状态压缩) 题面 给出一棵树和一个图,点数均为n,问有多少种方法把树的节点标号,使得对于树上的任意两个节点u,v,若树上u ...

  5. hdu 4272 LianLianKan 状态压缩

      LianLianKan Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  6. HDU 5724 Chess (状态压缩sg函数博弈) 2016杭电多校联合第一场

    题目:传送门. 题意:有n行,每行最多20个棋子,对于一个棋子来说,如果他右面没有棋子,可以移动到他右面:如果有棋子,就跳过这些棋子移动到后面的空格,不能移动的人输. 题解:状态压缩博弈,对于一行2^ ...

  7. hdu 1429(bfs+状态压缩)

    题意:容易理解,但要注意的地方是:如果魔王回来的时候刚好走到出口或还未到出口都算逃亡失败.因为这里我贡献了一次wa. 分析:仔细阅读题目之后,会发现最多的钥匙数量为10把,所以把这个作为题目的突破口, ...

  8. hdu 2489 最小生成树状态压缩枚举

    思路: 直接状态压缩暴力枚举 #include<iostream> #include<algorithm> #include<cstdio> #include< ...

  9. LianLianKan - HDU 4272(状态压缩)

    题目大意:有一列数据,可以从最上面的开始连接下面相同的元素,然后消除,不过距离不能超过6,询问最后能不能消除完整个数列. 分析:首先讨论一点最远能消除的地方,比如点的位置是x,如若想要消除x+1位置处 ...

随机推荐

  1. Linux安装OpenCV

    sudo apt-get update sudo apt-get install git git clone https://github.com/jayrambhia/Install-OpenCV ...

  2. python 取余运算

    python中取余运算逻辑如下: 如果a 与d 是整数,d 非零,那么余数 r 满足这样的关系: a = qd + r , q 为整数,且0 ≤ |r| < |d|. 经过测试可发现,pytho ...

  3. Geode 集群搭建,快速上手使用

    Geode 介绍: Geode是一个提供实时.一致访问大型分布式云平台下数据密集型应用的数据管理平台. Geode 通过跨多进程,把内存.CPU.网络资源和可选的本地磁盘汇集起来,来管理应用程序对象及 ...

  4. aoj-0118 property distribution(搜索)

    Time limit1000 ms Memory limit131072 kB タナカ氏が HW アールの果樹園を残して亡くなりました.果樹園は東西南北方向に H × Wの区画に分けられ.区画ごとにリ ...

  5. python——全局变量&局部变量

    >>> count = 5 >>> def function(): count = 10 print(10) >>> function() 10 ...

  6. Java-创建一个线程

    第一种继承Thread类 package com.tj; public class BasicThread1 extends Thread { public void run() { System.o ...

  7. Dijkstra算法_北京地铁换乘_android实现-附带源码.apk

    Dijkstra算法_北京地铁换乘_android实现   android 2.2+ 源码下载    apk下载 直接上图片 如下: Dijkstra(迪杰斯特拉)算法是典型的最短路径路由算法,用于计 ...

  8. 基于EPOLL模型的局域网聊天室和Echo服务器

    一.EPOLL的优点 在Linux中,select/poll/epoll是I/O多路复用的三种方式,epoll是Linux系统上独有的高效率I/O多路复用方式,区别于select/poll.先说sel ...

  9. Selenium WebDriver- 使用显示等待,判断搜狗的输入框是否显示,按钮是否可点击,然后在输入光荣之路搜索词,然后在点击搜索。

    #encoding=utf-8 from selenium import webdriver import time from selenium.webdriver.common.by import ...

  10. day03_12 缩进介绍

    python比较变态,必须缩进,而C可以不缩进,世界上只有python这门语言要求必须缩进 tab键是缩进,shift+tab往左移动 SyntaxError:invalid syntax 语法错误 ...