hdu1796 How many integers can you find
来源:互联网 发布:嫁给程序员的弊端 编辑:程序博客网 时间:2024/05/14 07:01
原题:http://acm.hdu.edu.cn/showproblem.php?pid=1796
用到了容斥原理。看到了很好的算法,直接采用了。
#include<iostream>#include<cstdio>#include<cstring>using namespace std;typedef __int64 int64;int n, m, cnt, i, j, x;int64 ans, a[30];int64 gcd(int64 a, int64 b){return b == 0 ? a : gcd(b, a % b);}//id 为1 的时候, 计算本身集合的数量。id为2的时候, 计算重叠;//反复加减, 不计算后面的情况,有点DP的味道。void DFS(int cur, int64 lcm, int id)//i, a[i], 1{lcm = a[cur] / gcd(a[cur], lcm) * lcm;if (id & 1)ans += (n - 1) / lcm; //因为这题并不包含n本身,所以用n-1elseans -= (n - 1) / lcm;for (int i = cur + 1; i < cnt; i++)DFS(i, lcm, id + 1);}int main(){while (~scanf("%d%d", &n, &m))//~end of file{cnt = 0;ans = 0;while (m--){scanf("%d", &x);if (x != 0) //除0的情况a[cnt++] = x;}for (i = 0; i < cnt; i++)DFS(i, a[i], 1);printf("%d\n", ans);}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 (容斥原理)
- js中文乱码的解决方法
- codeforces Round #241(div2) C解题报告
- 写给看着题干着急的学生
- 打成jar包
- 不管结果,只管过程。
- hdu1796 How many integers can you find
- 替换字符串中特征字符串为指定字符串
- 百度上线安全联盟侵权举报中心
- 《C++ Primer》学习之类访问权限控制
- IOS中CGAffineTransform的使用方法
- vim入门学习之常用快捷键
- 第7周作业1-循环大战
- 如何在LINUX增加对NTFS的操作
- Mysql,我为了面试准备的