题意:给定一个数 n,和一个集合 m,问你小于的 n的所有正数能整除 m的任意一个的数目。

析:简单容斥,就是 1 个数的倍数 - 2个数的最小公倍数 + 3个数的最小公倍数 + ...(-1)^(n+1) * n个数的最小公倍数。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 10 + 10;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} int a[maxn];
int lcm(int a, int b){
return a * (b / gcd(a, b));
} int main(){
while(scanf("%d %d", &n, &m) == 2){
for(int i = 0; i < m; ++i) scanf("%d", a+i);
int all = 1<<m;
int ans = 0;
--n;
for(int i = 1; i < all; ++i){
int cnt = 0, l = 1;
for(int j = 0; j < m; ++j) if(i&(1<<j)){
++cnt;
l = lcm(l, a[j]);
}
if(l == 0) continue;
ans += (cnt&1) ? n / l : - n / l;
} printf("%d\n", ans);
}
return 0;
}

  

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

  1. hdu 1796 How many integers can you find 容斥定理

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  2. hdu 1796 How many integers can you find 容斥第一题

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  3. HDU 1796 How many integers can you find 容斥入门

    How many integers can you find Problem Description   Now you get a number N, and a M-integers set, y ...

  4. HDU.1796 How many integers can you find ( 组合数学 容斥原理 二进制枚举)

    HDU.1796 How many integers can you find ( 组合数学 容斥原理 二进制枚举) 题意分析 求在[1,n-1]中,m个整数的倍数共有多少个 与 UVA.10325 ...

  5. HDU 1796 How many integers can you find (状态压缩 + 容斥原理)

    题目链接 题意 : 给你N,然后再给M个数,让你找小于N的并且能够整除M里的任意一个数的数有多少,0不算. 思路 :用了容斥原理 : ans = sum{ 整除一个的数 } - sum{ 整除两个的数 ...

  6. HDU 1796 How many integers can you find(容斥原理)

    题目传送:http://acm.hdu.edu.cn/diy/contest_showproblem.php?cid=20918&pid=1002 Problem Description    ...

  7. HDU 1796 How many integers can you find(容斥原理+二进制/DFS)

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  8. HDU How many integers can you find 容斥

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  9. How many integers can you find(容斥+dfs容斥)

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

随机推荐

  1. C++中预定义的宏

    以下信息摘自与标准C++的文档中. 如果把这些宏加在程序的日志中,它将为开发人员进行问题分析提供了很好的帮助. standard c++ 1998版The following macro names ...

  2. bzoj3597 方伯伯运椰子

    有一个 DAG,有一个源点,一个汇点和很多条边,每条边有花费 $d_i$ 和最大流量 $c_i$,可以花 $b_i$ 的钱把最大流量增加 $1$,花 $a_i$ 的钱把最大流量减少 $1$ 现在要进行 ...

  3. GO语言heap剖析及利用heap实现优先级队列

    GO语言heap剖析 本节内容 heap使用 heap提供的方法 heap源码剖析 利用heap实现优先级队列 1. heap使用 在go语言的标准库container中,实现了三中数据类型:heap ...

  4. shell while的用法

    1. #!/bin/shint=1while (( "$int < 10" ))doecho "$int"let "int++"don ...

  5. 隐藏select中的“请选择”项

    <select> <option value="" style="display: none">请选择</option> & ...

  6. ubuntu下vi文本后出现不正常的情况

    安装vim full版本由于Ubuntu预安装的是tiny版本,所以会导致我们在使用上的产生上述的不便.但是,我们安装了vim的full版本之后,键盘的所有键在vi下就很正常了.首先,要先卸掉旧版的v ...

  7. [转]JavaScript之引用类型

    Object类型 Object类型是JavaScript中使用最多的一种类型.虽然Object的实例不具备多少功能,但对于在应用程序中存储和传输数据而言,它确实是非常理想的选择. 创建Object实例 ...

  8. BZOJ2565:最长双回文串

    浅谈\(Manacher\):https://www.cnblogs.com/AKMer/p/10431603.html 题目传送门:https://www.lydsy.com/JudgeOnline ...

  9. Loadrunner-场景设置以及监控结果分析

    一.Controller的基本工作原理:通过1.2.3设置来模拟用户的操作,收集出4的各种信息 二.场景设置一般步骤 1.新建场景(Controller) 2.添加脚本 3.设置Schedule(设置 ...

  10. CentOS 7 安装Percona,Xtrabackup

    CentOS 7 安装Percona 5.7,Xtrabackup 简介 Percona Server为 MySQL 数据库服务器进行了改进,在功能和性能上较 MySQL 有着很显著的提升.该版本提升 ...