[容斥原理] 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 ...
随机推荐
- (四)Mybatis总结之接口映射
前面Mybatis是直接通过Dao层与数据交互,更好的方法是Mybatis通过接口映射方式与数据交互 1.在项目中添加maven支持(即pom.xml下添加支持) <!-- 在pom.xml下配 ...
- iOS规范化时间格式,object-C计算指定时间与当前的时间差
object-c计算指定时间与当前的时间差 头文件(.h): #import <Foundation/Foundation.h> @interface LuDate : NSDate +( ...
- 总结esp8266刷Python的完整的步骤(终极总结)
2018-04-0319:12:02 从玩microPython 到现在,一路荆棘一路坎坷. 不知道只有我遇到这样的问题还是microPython太不稳定,还是我买的板子太糙.总之遇到了太多问题了. ...
- 关于使用Axis2 webservice 处理Fault响应时抛org.apache.axis2.AxisFault的分析
使用Axis2这个框架进行webservice协议通讯,期间出了个问题,我(CLIENT)请求后,当服务端返回符合协议的SOAP异常报文,例如<soap:fault> ... 我的程序直接 ...
- scala学习(2)---option空值处理
https://blog.csdn.net/shadowsama/article/details/78148919 https://www.cnblogs.com/mustone/p/5648914. ...
- 学习SpringBoot中遇见的坑
1. 在搭建SpringBoot HelloWorld 时项目结构应该这样: 而不能这样: 否则访问时出现错误页面: 原因:此时还不知道,先记录下来. --已解决2018/12/11,因为Spring ...
- php部分基础
变量使用$,如$num = 1; 或 $name = 'hey'; 创建数组:$arr = array('a','b','c'); 或 $arr = array('a' => $name); 取 ...
- spring 中属性scope 的prototype(有状态)和singleton(无状态)
默认情况下,从bean工厂所取得的实例为Singleton(bean的singleton属性) Singleton: Spring容器只存在一个共享的bean实例, 默认的配置. Prototype: ...
- xmpp获取好友信息和添加删除好友(4)
原始地址: XMPPFrameWork IOS 开发(五)获取好友信息和添加删除好友 好友列表和好友名片 [_xmppRoster fetchRoster];//获取好友列表 //获取到一个好友节点 ...
- NOIp2017——追求那些我一直追求的
谨以此祭奠我即将爆炸的NOIP2017. $Mingqi\_H\ \ 2017.09.24$ Day -47 突然发现半年来自己从来没有写对过SPFA,最近几天才发现自己的板子一直是错的...赶紧找个 ...