HDU1796 How many integers can you find(容斥原理)
题目给一个数字集合,问有多少个小于n的正整数能被集合里至少一个元素整除。
当然是容斥原理来计数了,计算1个元素组合的有几个减去2个元素组合的LCM有几个加上3个元素组合的LCM有几个。注意是LCM。
- 而[1,n]中能被x整除的数字有$ \lfloor \frac nx \rfloor$个,因为设有$t$个,$x \times t \leqslant n$。
- 计算多个数LCM利用:$lcm(a,b)=a/gcd(a,b)\times b $,$lcm(a,b,c)=lcm(a,lcm(b,c))$
#include<cstdio>
#include<cstring>
using namespace std;
int gcd(int a,int b){
if(b==) return a;
return gcd(b,a%b);
}
int lcm(int a,int b){
return a/gcd(a,b)*b;
}
int getCnt(int s){
int res=;
for(int i=; i<; ++i){
if((s>>i)&) ++res;
}
return res;
}
int main(){
int n,m,a[];
while(~scanf("%d%d",&n,&m)){
--n;
int t=;
while(m--){
scanf("%d",&a[t]);
if(a[t]) ++t;
}
m=t;
int ans=;
for(int i=; i<(<<m); ++i){
int res=;
for(int j=; j<m; ++j){
if(a[j]== || ((i>>j)&)==) continue;
res=lcm(res,a[j]);
}
if(getCnt(i)&) ans+=n/res;
else ans-=n/res;
}
printf("%d\n",ans);
}
return ;
}
HDU1796 How many integers can you find(容斥原理)的更多相关文章
- hdu1796 How many integers can you find 容斥原理
Now you get a number N, and a M-integers set, you should find out how many integers which are small ...
- HDU 1796 Howmany integers can you find (容斥原理)
How many integers can you find Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 ...
- Hdu1796 How many integers can you find 2017-06-27 15:54 25人阅读 评论(0) 收藏
How many integers can you find Time Limit : 12000/5000ms (Java/Other) Memory Limit : 65536/32768K ...
- 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 ...
- hdu1796 How many integers can you find
//设置m,Q小于n可以设置如何几号m随机多项整除 //利用已知的容斥原理 //ans = 数是由数的数目整除 - 数为整除的两个数的数的最小公倍数 + 由三个数字... #include<cs ...
- HDU 1796 How many integers can you find(容斥原理)
题意 就是给出一个整数n,一个具有m个元素的数组,求出1-n中有多少个数至少能整除m数组中的一个数 (1<=n<=10^18.m<=20) 题解 这题是容斥原理基本模型. 枚举n中有 ...
- HDU1796 How many integers can you find【容斥定理】
题目链接: http://acm.hdu.edu.cn/showproblem.php? pid=1796 题目大意: 给你一个整数N.和M个整数的集合{A1.A2.-.Am}.集合内元素为非负数(包 ...
- 容斥原理学习(Hdu 4135,Hdu 1796)
题目链接Hdu4135 Co-prime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- hdu分类 Math Theory(还有三题!)
这个分类怎么觉得这么水呢.. 这个分类做到尾的模板集: //gcd int gcd(int a,int b){return b? gcd(b, a % b) : a;} //埃氏筛法 O(nlogn) ...
随机推荐
- 在Android的webview中定做js的alert,confirm和prompt对话框的方法
在Android的webview中定制js的alert,confirm和prompt对话框的方法 http://618119.com/archives/2010/12/20/199.html 1.首先 ...
- unity3d webplayer 16:9 居中显示模板
原地址:http://www.cnblogs.com/88999660/archive/2013/04/12/3016773.html <!DOCTYPE html PUBLIC "- ...
- 以Python角度学习Javascript(二)之DOM
HTML DOM 定义了访问和操作 HTML 文档的标准方法. DOM 将 HTML 文档表达为树结构. 文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文 ...
- 默认hosts后面为files dns
售后工程师 : 您好,问题已经解决,问题原因是您修改了/etc/nsswitch.conf配置文件中的hosts:这项导致的,默认hosts后面为files dns,但是后面去掉了DNS导致直接使用本 ...
- c++11 gcc4.8.x安装
c++11 gcc4.8.x安装 2014年12月11日默北 要安装PHP-CPP,需要c++11,否则就会报错,错误信息如下所示: g++ -Wall -c -g -std=c++11 -fpi ...
- Linux时间同步配置方法
由于是在做mongoDB的实验中再一次的遇到了mongos路由节点同步时由于ntp时间的问题导致同步非常的慢.故写了个时间同步的语句===> while :; do rdate -s 192.1 ...
- Android 模拟器genymotion安装,eclipse 插件
genymotion是一款号称速度最快性能最好的android模拟器,它基于Oracle VM VirtualBox.支持GPS.重力感应.光.温度等诸多传感器:支持OpenGL 3D加速:电池电量模 ...
- Java for LeetCode 047 Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- Greedy:Protecting the Flowers(POJ 3262)
保护花朵 题目大意:就是农夫有很多头牛在践踏花朵,这些牛每分钟破坏D朵花,农夫需要把这些牛一只一只运回去,这些牛各自离牛棚都有T的路程(有往返,而且往返的时候这只牛不会再破坏花),问怎么运才能使被践踏 ...
- Enum:Hopscotch(POJ 3050)
跳格子 题目大意:牛像我们一样跳格子,一个5*5的方格,方格有数字,给牛跳5次,可以组成一个6个数字组合字符串,请问能组合多少个字符串? 题目规模很小,暴力枚举,然后用map这个玩具来检测存不存在就可 ...