[容斥原理] hdu 1796 How many integers can you find
题意:
给一个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的更多相关文章
- HDU.1796 How many integers can you find ( 组合数学 容斥原理 二进制枚举)
HDU.1796 How many integers can you find ( 组合数学 容斥原理 二进制枚举) 题意分析 求在[1,n-1]中,m个整数的倍数共有多少个 与 UVA.10325 ...
- HDU 1796 How many integers can you find (状态压缩 + 容斥原理)
题目链接 题意 : 给你N,然后再给M个数,让你找小于N的并且能够整除M里的任意一个数的数有多少,0不算. 思路 :用了容斥原理 : ans = sum{ 整除一个的数 } - sum{ 整除两个的数 ...
- HDU 1796 How many integers can you find(容斥原理)
题目传送:http://acm.hdu.edu.cn/diy/contest_showproblem.php?cid=20918&pid=1002 Problem Description ...
- 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 ...
- 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 ...
- HDU 1796 How many integers can you find(容斥原理)
题意 就是给出一个整数n,一个具有m个元素的数组,求出1-n中有多少个数至少能整除m数组中的一个数 (1<=n<=10^18.m<=20) 题解 这题是容斥原理基本模型. 枚举n中有 ...
- 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 ...
- hdu 1796 How many integers can you find
容斥原理!! 这题首先要去掉=0和>=n的值,然后再使用容斥原理解决 我用的是数组做的…… #include<iostream> #include<stdio.h> #i ...
- 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 ...
随机推荐
- Java编程思想读书笔记_第8章
覆盖私有方法 class Father { private void f() { System.out.println("Father::f()"); } public stati ...
- pycharm永久激活(转载)
转载自CSDN--http://blog.csdn.net/mr_hhh/article/details/79062747 2018-02-2417:30:52 今天再补充一个教程,关于pycharm ...
- JSP学习笔记 - 内置对象 Response
1.response.addHeader("refresh","2"): 制定页面刷新时间 2.response.addHeader("refresh ...
- jQuery,遍历表格每个单元格数据。
<table class="table table-hover table-bordered"> <thead> <tr> <th > ...
- CAD使用GetAllAppName读所有名称(网页版)
主要用到函数说明: MxDrawEntity::GetAllAppName 得到所有扩展数据名称,详细说明如下: 参数 说明 [out, retval] IMxDrawResbuf** ppRet 返 ...
- CAD使用GetxDataLong读数据(网页版)
主要用到函数说明: MxDrawEntity::GetxDataLong2 读取一个Long扩展数据,详细说明如下: 参数 说明 [in] LONG lItem 该值所在位置 [out, retval ...
- Java怎么实现文件数据拷贝
实现一个文件的内容拷贝到另一个文件里 public void copyDemo () throws IOException { //创建文件输入流 FileInputStream fis = new ...
- 07C语言程序语句
C语言程序语句 判断语句 if(表达式) {语句} #include <stdio.h> int main(){ printf("请输入2个数字:"); int a,b ...
- Linux:RHEL7图形界面转文字
1.7 Systemd初始化进程 Linux操作系统开机过程首先从BIOS开始→进入"Boot Loader"→加载系统内核→内核进行初始化→启动初始化进程.初始化进程作为系统第一 ...
- 类与类之间的关系UML模型图
关联.依赖.聚合.组合.泛化.实现 类之间可能存在以下几种关系:关联(association).依赖(dependency).聚合(Aggregation,也有的称聚集).组合(Composition ...