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中有 ...
随机推荐
- iOS 删除相册中照片--来自简书
来自:http://www.jianshu.com/p/ac18aa3f28c2 最近公司的app有一个新功能是在app中删除相册的照片 ,本来是一个比较简单地功能,在做的过程中却发现AssetsLi ...
- html 表格中文字的背景色
- Modulo Sum(背包 + STL)
Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- Oracle 取上周一到周末日期的查询语句
-- Oracle 取上周一到周末的sql -- 这样取的是 在一周内第几天,是以周日为开始的 select to_char(to_date('20130906','yyyymmdd'),'d') f ...
- 忘记root密码修改方法
好吧,不想重新装(那耗费的时间真心伤不起…),找修改root密码的方法,结果还真找到了… 首先在虚拟机启动时,在Grub界面按“e”进入编辑 在linux /vmlinuz那行后面添加“init=/b ...
- 淘宝开源任务调度框架tbschedule
背景 分布式任务调度是非常常见的一种应用场景,一般对可用性和性能要求不高的任务,采用单点即可,例如linux的crontab,spring的quarz,但是如果要求部署多个节点,达到高可用的效果,上面 ...
- exchange邮箱的”单点登陆“
在跟exchange集成登陆时,通常有这样的需求,用户需要点击邮件链接的时候直接打开,不再需要输入用户名密码,实现所谓的单点登陆. 通常有两种方式 1.form认证 登陆原理:用js模拟表单登陆 代码 ...
- 定时排程刷新微信access-token
微信公众号开发中最常遇到的就是调用接口时候需要有API的access-token(非网页授权的access-token),有了这个token之后,才可以发生模板消息等.这里的做法主要是用nodejs的 ...
- C#创建Windows服务与安装-图解
1.创建windows服务项目
- java中关于String 类型数据 的存储方式
Constant Pool常量池的概念: 在讲到String的一些特殊情况时,总会提到String Pool或者Constant Pool,但是我想很多人都不太 明白Constant Pool到底是个 ...