题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1796
题目的意思: 让你找 1 - (n - 1)内,能够被一个序列中任意的数整除的个数。。- - 经典的容斥原理。。
需要注意的几点:
1.序列中的0需要去掉。。
2.过程中超过int。。自己傻b的竟然认为不会超过 - -。。。。。
然后就没有了。。经典的容斥。。。
Code:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
using namespace std;
#define INT long long
const int N = 12;
INT p[N];
INT gcd(INT a, INT b)
{
if(b == 0) return a;
return gcd(b, a % b);
}
INT lcm(INT a, INT b)
{
return a * b / gcd(a, b);
}
INT solve(INT n, INT m)
{
INT ans = 0;
for(INT i = 1; i < (1 << m); i ++){
INT cnt = 0, tmp = 1;
for(INT j = 0; j < m; j ++){
if(i & (1 << j)){
cnt ++;
tmp = lcm(tmp, p[j]);
}
}
if(cnt % 2) ans += n / tmp;
else ans -= n / tmp;
}
return ans;
}
int main()
{
INT n, m;
while(cin >> n >> m){
INT t = 0, x;
for(INT i = 0; i < m; i ++){
cin >> x;
if(x > 0) p[t ++] = x;
}
m = t;
cout << solve(n - 1, m) << endl;
}
return 0;
}
----->
越来越发现冗词原理的有趣性。。也许可能是学习的乐趣性。。加油!! 加油!!