UVA
来源:互联网 发布:原生js手风琴效果 编辑:程序博客网 时间:2024/06/05 09:40
题目:
代码:
#include<iostream>using namespace std;int gcd(int a, int b){if (b)return gcd(b, a%b);return a;}int main(){int n, m, l[15], s;while (cin >> n >> m){s = 0;for (int i = 0; i < m; i++)cin >> l[i];for (int k = 0; k < (1 << m); k++){long long r = 1, b = 0;for (int i = 0; i < m; i++)if (k&(1 << i)){if (r <= n)r *= l[i] / gcd(l[i], r);b++;}if (b % 2)s -= n/r;else s += n/r;}cout << s << endl;}return 0;}
用了状态压缩来枚举2^m种状态,用k对应状态
注意一个细节,当r超过n时,r/n就已经是0了,就不要再继续求lcm了,否则会越界,因为多个数的lcm很容易超过long long的范围
阅读全文
0 0
- uva
- UVA
- UVA
- UVA
- uva
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- 使用AJXA
- python中的深拷贝和浅拷贝理解
- 不同内存区域的耗时操作
- 对象中的constructor
- 这是一份很详细的 Retrofit 2.0 使用教程(含实例讲解)
- UVA
- 方差、标准差、均方差、均方误差区别总结
- 【Machine Learning】【Python】四、Hard Negative Mining优化训练SVM模型 ---- 《SVM物体分类和定位检测》
- Javascript数组的排序 sort()方法和reverse()方法
- hibernate的QBE和QBC
- 为体验设计——使用第一
- UVA10391 ZOJ1825 Compound Words【SET+暴力】
- JSON详解
- 用户体验和网站