HDU1796 How many integers can you find(容斥)
来源:互联网 发布:网络录音电话 编辑:程序博客网 时间:2024/05/21 15:38
题意
给定a1,a2…am,求小于n的正整数至少能整除a中一个元素的数有几个?
思路
求[1,r)内有因子出现在集合中的数的个数。题目要求的数要么含有一个集合中的因子,要么是两个,要么是三个..要么是m个,而含两个集合中因子的数在计算含有一个集合中的因子,被重复计算了,三个时候也在计算二个的时候被重复计算,所以需要用到容斥原理。2^m枚举集合中元素,然后计算出最小公倍数,n/LCM就是1..n中含我们枚举的因子的数的个数对于求出来的数根据枚举到素因子个数奇数加偶数加。
这题有一个很坑的地方,a序列里有可能出现0,还有不包括n本身。。。被这个坑了好久。。。。这里枚举我直接二进制枚举,和状态压缩的道理差不多。。。
代码
#include <cstdio>#include <iostream>#define ll long longusing namespace std;const int kMaxn = 30 + 5;ll a[kMaxn];ll Gcd(ll x, ll y) { if(x % y == 0) return y; return Gcd(y, x % y);}int main() { int n,m; while(~scanf("%d %d", &n, &m)) { n--; int t = 0; for(int i = 0; i < m; i++) { ll x; scanf("%lld", &x); if(x != 0) a[t++] = x; } m = t; ll res = 0; for(int i = 1; i < (1 << m); i++) { int num = 0; for(int j = i; j != 0; j = j >> 1) num += j & 1; ll lcm = 1; for(int j = 0; j < m; j++) { if(i >> j & 1) { //printf("%d ", a[j]); lcm = lcm / Gcd(lcm, a[j]) * a[j]; if(lcm > n) break; } } //printf("\n"); if(num % 2 == 0) res -= n / lcm; else res += n / lcm; } printf("%lld\n", res); } return 0;}
0 0
- hdu1796 How many integers can you find----容斥原理
- hdu1796--How many integers can you find--容斥原理
- hdu1796---How many integers can you find(容斥原理)
- HDU1796 How many integers can you find【容斥定理】
- 容斥定理 hdu1796 How many integers can you find
- hdu1796 How many integers can you find 容斥原理
- hdu1796 How many integers can you find 容斥原理
- 【容斥原理】HDU1796 How many integers can you find
- hdu1796 How many integers can you find(容斥)
- HDU1796 How many integers can you find[容斥定理]
- HDU1796 How many integers can you find (容斥原理)
- HDU1796 How many integers can you find(容斥)
- hdu1796 How many integers can you find (容斥原理)
- hdu1796 How many integers can you find
- HDU1796-How many integers can you find
- hdu1796 How many integers can you find
- Hdu1796 How many integers can you find
- hdu1796--How many integers can you find(容斥原理)
- 好坑的题目
- 二进制与16进制的对应关系
- 算法导论之线性时间排序(3)
- 梯度(Gradient Descent) 方向梯度 (directional derivative) 以及 gradient descent梯度下降算法(1)
- hdu5831 Rikka with Parenthesis II 2016 Multi-University Training Contest 8 解题报告
- HDU1796 How many integers can you find(容斥)
- Linux的根文件系统
- 20-读写空指针
- ucos-II(五)
- JAVA进阶6.4——线程的睡眠
- 剑指offer----跳台阶
- 对cmd的使用(新建,覆盖,追加,删除)
- java多线程编程2--对象及变量的并发访问
- shell详解四