题意

就是给出一个整数n,一个具有m个元素的数组,求出1-n中有多少个数至少能整除m数组中的一个数

(1<=n<=10^18.m<=20)

题解

这题是容斥原理基本模型。

枚举n中有多少m中元素的个数,在结合LCM考虑容斥。

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const long long N=;
long long a[N],n,m,ans;
long long gcd(long long x,long long y){
if(y==)return x;
else return gcd(y,x%y);
}
long long lcm(long long x,long long y){
return x/gcd(x,y)*y;
}
void dfs(long long now,long long num,long long res,long long k){
if(res==){
if(num==)return;
long long tmp=n/num;
if(k&)ans+=tmp;
else ans-=tmp;
return;
}
if(now==m+)return ;
if(lcm(num,a[now])<=n)dfs(now+,lcm(num,a[now]),res-,k);
dfs(now+,num,res,k);
}
int main(){
while(scanf("%lld%lld",&n,&m)!=EOF){
n--;
for(long long i=;i<=m;i++)scanf("%lld",&a[i]);
for(long long i=;i<=m;i++)dfs(,,i,i);
printf("%lld\n",ans);
}
}

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 ( 组合数学 容斥原理 二进制枚举)

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

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

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

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

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

  5. 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 ...

  6. 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 ...

  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 1796 How many integers can you find 容斥第一题

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

  9. hdu 1796 How many integers can you find

    容斥原理!! 这题首先要去掉=0和>=n的值,然后再使用容斥原理解决 我用的是数组做的…… #include<iostream> #include<stdio.h> #i ...

随机推荐

  1. nginx 日志配置不生效的问题

    log_format 有个默认的日志格式: log_format combined '$remote_addr - $remote_user [$time_local] ' ' "$requ ...

  2. JQuery中text(),html(),val()的区别

    这3个都是jquery类库中的语法,分别是: text():获取或者改变指定元素的文本: html():获取或改变指定元素的html元素以及文本: val():获取或者改变指定元素的value值(一般 ...

  3. Eclipse中使用GIT更新项目

    GIT更新项目: 右击项目——Team——Pull:

  4. TensorFlow+实战Google深度学习框架学习笔记(5)----神经网络训练步骤

    一.TensorFlow实战Google深度学习框架学习 1.步骤: 1.定义神经网络的结构和前向传播的输出结果. 2.定义损失函数以及选择反向传播优化的算法. 3.生成会话(session)并且在训 ...

  5. HDU 1061 Rightmost Digit( 快速幂水 )

    链接:传送门 题意:求 N^N 的个位 思路:快速幂水题 /********************************************************************** ...

  6. UVA272-TEX Quotes(紫书例题3.1)

    TeX is a typesetting language developed by Donald Knuth. It takes source text together with a few ty ...

  7. BZOJ 1717 [USACO06DEC] Milk Patterns (后缀数组+二分)

    题目大意:求可重叠的相同子串数量至少是K的子串最长长度 洛谷传送门 依然是后缀数组+二分,先用后缀数组处理出height 每次二分出一个长度x,然后去验证,在排序的后缀串集合里,有没有连续数量多于K个 ...

  8. 洛谷P5269 欧稳欧再次学车

    正常模拟就好~ 首先初始化:转速=l, 档位=1 然后读入数据 由于先要处理换挡操作,所以我们先按照x处理,再按照y处理 当x=0时,档位+1,转速=l 当x=1时,档位-1,转速=r 当y=1时,转 ...

  9. HTML5 基础测试题

          HTML5 基础测试题 1.HTML5 之前的 HTML 版本是什么?() A.HTML 4.01 B.HTML 4 C.HTML 4.1 D.HTML 4.9 2.HTML5 的正确 d ...

  10. Ajax兼容性问题

    对于IE7及以上直接使用 XMLHttpRequest 就行,但对于过老版本IE建议直接提示用户下载新版浏览器更佳.或者用以下代码兼容IE6: function CreateXHR() { if(XM ...