HDU.1796 How many integers can you find ( 组合数学 容斥原理 二进制枚举)
HDU.1796 How many integers can you find ( 组合数学 容斥原理 二进制枚举)
题意分析
求在[1,n-1]中,m个整数的倍数共有多少个
与 UVA.10325 The Lottery 一模一样。
前置技能和其一样,但是需要注意的有一下几点:
1. m个数字中可能有0
2. 要用long long
代码总览
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#define nmax 20
#define ll __int64
using namespace std;
ll initnum[nmax];
ll n;
int m;
ll gcd(ll a, ll b)
{
if(!b) return a;
else return gcd(b, a%b);
}
ll lcm(ll a, ll b)
{
return a/abs(gcd(a,b))*b;
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%I64d %d",&n,&m) != EOF){
int num = 0;
ll temp = 0;
for(int i = 0 ;i<m;++i){
scanf("%I64d",&temp);
if(temp !=0) initnum[num++] = temp;
}
ll time = (1<<num);
ll ans = 0;
n--;
for(int i = 1; i<time;++i){
int index = 0;
ll tmpans = 1LL;
for(int j = 0; j<num;++j){
if( 1 & (i>>j)){
tmpans = lcm(tmpans,initnum[j]);
index++;
}
}
if(index & 1){//add
ans += n / tmpans;
}else{//even
ans -= n / tmpans;
}
}
//ans = n-ans;
printf("%I64d\n",ans);
}
return 0;
}
HDU.1796 How many integers can you find ( 组合数学 容斥原理 二进制枚举)的更多相关文章
- 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(容斥原理)
题目传送:http://acm.hdu.edu.cn/diy/contest_showproblem.php?cid=20918&pid=1002 Problem Description ...
- HDU 1796 How many integers can you find (状态压缩 + 容斥原理)
题目链接 题意 : 给你N,然后再给M个数,让你找小于N的并且能够整除M里的任意一个数的数有多少,0不算. 思路 :用了容斥原理 : ans = sum{ 整除一个的数 } - sum{ 整除两个的数 ...
- 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 容斥定理
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(容斥原理)
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 容斥第一题
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
容斥原理!! 这题首先要去掉=0和>=n的值,然后再使用容斥原理解决 我用的是数组做的…… #include<iostream> #include<stdio.h> #i ...
- HDU 1796 How many integers can you find 【容斥】
<题目链接> 题目大意: 给你m个数,其中可能含有0,问有多少小于n的正数能整除这个m个数中的某一个. 解题分析: 容斥水题,直接对这m个数(除0以外)及其组合的倍数在[1,n)中的个数即 ...
随机推荐
- 2.3 Oracle之DDL 语句(约束、伪列、视图、序列、同义词) 精简版
DDL Data Definition(重点) (n. 定义:[物] 清晰度:解说)用于定义数据的结构,创建,修改,删除数据库对象 一.表的增删改查 1.创建表:CREATE TABLE temp A ...
- 论文笔记:分形网络(FractalNet: Ultra-Deep Neural Networks without Residuals)
FractalNet: Ultra-Deep Neural Networks without Residuals ICLR 2017 Gustav Larsson, Michael Maire, Gr ...
- bcd引导Ubuntu
下面步骤就是创建Windows的启动项了. 以管理员身份打开CMD, 然后输入 bcdedit /create /d "ubuntu" /application bootsecto ...
- linux获得命令使用帮助
1. 内部命令: help CMD 2. 外部命令: CMD --help 3. 命令手册: manual(所有命令) man CMD 分章节: 1: 用户命令(User Commands - /bi ...
- 2018软工实践—Beta冲刺(2)
队名 火箭少男100 组长博客 林燊大哥 作业博客 Beta 冲鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调组内工作 修改前端界面 展示GitHub当日代码/文档签入记录(组内 ...
- Internet History, Technology and Security (Week 9)
Week 9 Security: Web Security We are now on the second to last week of the class and finishing up ou ...
- 项目复审-Bata阶段
项目复审-Bata阶段 小组的名字和链接 优点 缺点 名次 别看了你没救了 https://www.cnblogs.com/liaoyujun233/p/9148781.html 基本功能都已经实现, ...
- Js apply方法详解,及其apply()方法的妙用
Js apply方法详解 我在一开始看到javascript的函数apply和call时,非常的模糊,看也看不懂,最近在网上看到一些文章对apply方法和call的一些示例,总算是看的有点眉目了,在这 ...
- ERROR 1698 (28000): Access denied for user 'root'@'localhost' 解决方法
之前MySQL服务端本机上使用密码登陆root账号是没有问题的,但是今天不知道是因为动了哪里,登陆失败并有这个错误代码: ~$ mysql -u root -p Enter password: ERR ...
- Redis&PHP的使用安装-windows版
Redis是一个Key-value的数据结构存储系统,可以以数据库的形式,缓存系统,消息处理器使用,它支持的存储value类型很多,例如,string.list(链表).set(集合).zset(so ...