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)中的个数即 ...
随机推荐
- Python函数初识二
一.变量的作用域LEGB 1.1.变量的作用域 在Python中,程序的变量并不是在哪个位置都可以访问的,访问权限决定于这个变量是在哪里赋值的.变量的作用域决定了在哪一部分程序可以访问哪个特定的变量名 ...
- Sublime Text Build System——编译运行Java
今天Google如何在ST中编译运行Java的时候,无意中发现了一个更好的方法. 其实,在ST中是可以编译Java的,但是运行不了,因为没有配置运行命令.那么一般的配置方法都是如下的: http:// ...
- zookeeper客户端相关命令
windows环境: 本机 直接 点机zkcli.cmd linux环境: 连接到zookeeper server ./zkCli.sh -server localhost:2181 help命 ...
- Scrum立会报告+燃尽图(十月十一日总第二次):需求分析
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2191 Scrum立会master:张俊余 一.小组介绍 组长:付佳 组员 ...
- centos6 安装图形化界面
1.首先进行光盘的挂载,注意光盘挂载时不会自动建立目录的,所以需要自己建立目录 mkdir /mnt/cdrom mount /dev/cdrom /mnt/cdrom #dev目录为设备目录 2.更 ...
- Task 6.1 校友聊之NABCD模型分析
我们团队开发的一款软件是“校友聊”--一个在局域网内免流量进行文字.语音.视频聊天的软件.下面将对此进行NABCD的模型分析. N(Need需求):现如今,随着网络的迅速普及,手机和电脑已经成为每个大 ...
- Task 3 求最大数组和
题目:返回一个整数数组中最大子数组的和. (要求:输入一个整形数组,数组里有正数也有负数. 数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和. 求所有子数组的和的最大值.要求时间复杂度为 ...
- 软工实践-Beta 冲刺 (7/7)
队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 1.界面的修改与完善 展示GitHub当日代码/文档签入记 ...
- Beat(2/7)
目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:beta冲刺(2/7) 团队部分 后敬甲(组长) 过去两天完成了哪些任务 整理博客 做了点商家数据表格 接下来的计划 做 ...
- 关于JoptionPane提示框
import java.util.*; import javax.swing.JOptionPane; import javax.swing.UIManager; public class Main ...