题意:

给一个N。然后给M个数,问1~N-1里面有多少个数能被这M个数中一个或多个数整除。

思路:

首先要N--

然后对于每一个数M 事实上1~N-1内能被其整除的 就是有(N-1)/M[i]个

可是会出现反复 比方 例子 6就会被反复算

这时候我们就须要容斥原理了

加上一个数的减去两个数的。。

这里要注意了 两个数以上的时候 是求LCM而不是简单的相乘!

代码:

#include "stdio.h"
#include "string.h"
#include "math.h"
#include "iostream"
#include "cstdlib"
#include "algorithm"
#include "queue"
using namespace std;
int a[12];
int used[12],b[12];
int n,m;
int gcd(int a,int b)
{
return b==0?a:gcd(b,a%b);
}
int lcm(int k)
{
int ans=b[0];
for(int i=1;i<k;i++)
{
int tep=gcd(ans,b[i]);
ans=ans/tep*b[i];
}
return ans;
}
__int64 dfs(int kk,int x,int lit)
{
__int64 ans=0;
if(x==lit)
{
int tep;
tep=lcm(x);
return n/tep;
}
for(int i=kk+1;i<m;i++)
{
if(a[i]==0) continue;
if(used[i]) continue;
used[i]=1;
b[x]=a[i];
ans+=dfs(i,x+1,lit);
used[i]=0;
}
return ans;
}
int main()
{
while(scanf("%d%d",&n,&m)!=-1)
{
n--;
for(int i=0;i<m;i++) scanf("%d",&a[i]);
__int64 ans=0;
for(int i=1;i<=m;i++)
{
// printf("%d\n",dfs(-1,0,i));
memset(used,0,sizeof(used));
if(i%2==0) ans-=dfs(-1,0,i);
else ans+=dfs(-1,0,i);
}
printf("%I64d\n",ans);
}
return 0;
}

[容斥原理] hdu 1796 How many integers can you find的更多相关文章

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

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

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

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

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

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

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

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

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

    题意 就是给出一个整数n,一个具有m个元素的数组,求出1-n中有多少个数至少能整除m数组中的一个数 (1<=n<=10^18.m<=20) 题解 这题是容斥原理基本模型. 枚举n中有 ...

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

  8. hdu 1796 How many integers can you find

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

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

随机推荐

  1. 使用jstack精确找到异常代码的

    https://blog.csdn.net/mr__fang/article/details/68496248

  2. 2057. [ZLXOI2015]殉国

    ★☆   输入文件:BlackHawk.in   输出文件:BlackHawk.out   评测插件 时间限制:0.05 s   内存限制:256 MB [题目描述] 正义的萌军瞄准了位于南极洲的心灵 ...

  3. Java:一个简捷的可分页的ResultSet实现

    内容 前言 JDBC和分页 和具体数据库相关的实现方法 另一种繁琐的实现方法 使用Vector进行分页 一个新的Pageable接口及其实现 Pageable的使用方法 总结 参考资料 关于作者 前言 ...

  4. golang zip 压缩,解压(含目录文件)

    每天学习一点go src. 今天学习了zip包的简单使用,实现了含目录的压缩与解压. 写了两个方法,实现了压缩.解压. package ziptest import ( "archive/z ...

  5. #NOIP前数学知识总结

    我好菜啊…… 欧拉函数 欧拉函数φ(n),是小于n且和n互质的正整数(包括1)的个数. 性质: 1.对于质数n: φ(n)=n-1 2..对于n=pk φ(n)=(p-1)*pk-1 3.积性函数的性 ...

  6. PHP爬数据 QueryList

    QueryList官方文档:https://www.querylist.cc/docs/guide/v3 因为php版本使用5.6,所以使用QueryList v3版本,php7可以使用 v4版本 v ...

  7. 牛客多校Round 1

    Solved:1 rank:249 E. Removal dp i,j表示前i个数删除了j个且选择了第i个的答案 类似字符串的dp 预处理一下nex i_k即i后面k第一次出现的位置  就好转移了 # ...

  8. 用Java写一个生产者-消费者队列

    生产者消费者的模型作用 通过平衡生产者的生产能力和消费者的消费能力来提升整个系统的运行效率,这是生产者消费者模型最重要的作用. 解耦,这是生产者消费者模型附带的作用,解耦意味着生产者和消费者之间的联系 ...

  9. Luogu P3802 小魔女帕琪

    P3802 小魔女帕琪 题目背景 从前有一个聪明的小魔女帕琪,兴趣是狩猎吸血鬼. 帕琪能熟练使用七种属性(金.木.水.火.土.日.月)的魔法,除了能使用这么多种属性魔法外,她还能将两种以上属性组合,从 ...

  10. 山建校赛B题公式证明

    原题 证明