HDU 1796 Howmany integers can you find (容斥原理)
How many integers can you find
Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5664 Accepted Submission(s): 1630
you get a number N, and a M-integers set, you should find out how many
integers which are small than N, that they can divided exactly by any
integers in the set. For example, N=12, and M-integer set is {2,3}, so
there is another set {2,3,4,6,8,9,10}, all the integers of the set can
be divided exactly by 2 or 3. As a result, you just output the number 7.
are a lot of cases. For each case, the first line contains two integers
N and M. The follow line contains the M integers, and all of them are
different from each other. 0<N<2^31,0<M<=10, and the M
integer are non-negative and won’t exceed 20.
2 3
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0);
#define maxn 500
__int64 k[maxn];
__int64 gcd(__int64 b,__int64 a)
{
return a==?b:gcd(a,b%a);
}
int main()
{
int n, m;
while(~scanf("%d%d", &n, &m))
{
int t;
n--;
int cnt = ;
for(int i = ; i < m; i++)
{
scanf("%d", &t);
if(t> && t < n)
k[cnt++] = t;
}
__int64 ans = ;
for(int i = ; i < <<cnt; i++)
{
int num = ;
__int64 lcm = ;
for(int j = ; j < cnt; j++)
{
if(i & ( << j))
{
num++;
lcm = k[j]/gcd(k[j], lcm) * lcm;
}
}
if(num & )
ans += n/lcm;
else
ans -= n/lcm;
}
printf("%I64d\n", ans);
}
return ;
}
2. 递归
#include<iostream>
#include<cstdio>
#include<algorithm> using namespace std;
__int64 a[];
int n,m;
//cur表示
__int64 sum=;
__int64 gcd(__int64 b,__int64 a)
{
return a==?b:gcd(a,b%a);
//while()
}//最小公倍数
void dfs(int cur,__int64 lcm,int id)//容斥原理公式
{
//lcm=lcm/gcd(lcm,a[cur])*a[cur];
lcm=a[cur]/gcd(a[cur],lcm)*lcm;
if(id&)//运用了快速幂的方法判断奇偶
sum+=(n-)/lcm;
else
sum-=(n-)/lcm;
// cout<<"id = "<<id<<" : "<<sum<<endl;
for(int i=cur+;i<=m;i++)
dfs(i,lcm,id+);
}
int main()
{
int t;
while(~scanf("%d%d",&n,&t))
{
int i,x;
m=;
for(i=;i<=t;i++)
{
scanf("%d",&x);
if(x)
{
a[++m]=x;
}
}
sum=;
for(i=;i<=m;i++)
dfs(i,a[i],);
printf("%I64d\n",sum);//容斥原理公式
// cout<<sum<<endl;
}
return ;
}
HDU 1796 Howmany 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 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- HDU 1796 容斥原理 How many integers can you find
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1796 处男容斥原理 纪念一下 TMD看了好久才明白DFS... 先贴代码后解释 #includ ...
- 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-1里面有多少个数能被这M个数中一个或多个数整除. 思路: 首先要N-- 然后对于每一个数M 事实上1~N-1内能被其整除的 就是有(N-1)/M[i]个 可是 ...
- HDU 1796 How many integers can you find(容斥原理)
题意 就是给出一个整数n,一个具有m个元素的数组,求出1-n中有多少个数至少能整除m数组中的一个数 (1<=n<=10^18.m<=20) 题解 这题是容斥原理基本模型. 枚举n中有 ...
随机推荐
- scheme 宏macro写法
scheme里的宏不同的实现有不同的写法: 1.mzscheme的define-macro (mzscheme也就是pltschme,也就是drracket,没有define-macro这个关键字) ...
- android系统的图片资源
使用系统的图片资源的好处有,一个是美工不需要重复的做一份已有的图片了,可以节约不少工时:另一个是能保证我们的应用程序的风格与系统一致. 1.引用方式 在源代码*.Java中可以进入如下方式引用: my ...
- UESTC_Sea Base Exploration CDOJ 409
When the scientists explore the sea base, they use a kind of auto mobile robot, which has the missio ...
- Unique Binary Search Trees II 解答
Question Given n, generate all structurally unique BST's (binary search trees) that store values 1.. ...
- 在国内使用cnpm代替npm
npm是Node.js的模块依赖管理工具,由于使用npm安装包是从国外服务器下载,在国内很容易受到网络的影响,速度非常慢,因此可以选用cnpm.cnpm可以使用淘宝团队提供的淘宝npm镜像,你可以用此 ...
- Struts2+Spring+Ibatis集成合并
上一篇博客讲述了Struts2+Spring的集成合并,主要是利用了一个中间jar包,这篇博客在加上Ibatis持久层框架,三个框架进行合并.其中Struts2和Spring部分和前边的一样,主要是讲 ...
- Java有用经验总结--Swing篇
Java有用经验总结--Swing篇 前言 本文前言部分为我的一些感想,假设你仅仅对本文介绍的Java有用技巧感兴趣,能够跳过前言直接看正文的内容. 本文的写作动机来源于近期接给人家帮忙写的一个小程序 ...
- 点击返回键退出popupwindow的方法
点击返回键退出popupwindow mPopupWindow.setFocusable(true); 这句非常重要,对背景不会有影响 mPopupWindow.setBackgroundDrawab ...
- 多媒体封装格式----mkv
Matroska 开源多媒体容器标准.MKV属于其中的一部分.Matroska常见的有.MKV视频格式.MKA音频格式..MKS字幕格式..MK3D files (stereoscopic/3D vi ...
- Html5 Css实现方形图片 圆形显示
<!doctype html><html><head><meta charset="utf-8"><title>方形图片 ...