51nod1284容斥定理

基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题
 
给出一个数N,求1至N中,有多少个数不是2 3 5 7的倍数。 例如N = 10,只有1不是2 3 5 7的倍数。
Input
输入1个数N(1 <= N <= 10^18)。
Output
输出不是2 3 5 7的倍数的数共有多少。
Input示例
10
Output示例
1
经典的容斥定理,
公式 |AUBUC|=|A|+|B|+|C|-|A^B|-|A^C|-|B^C|+|A^B^C|;
即等式左边是集合的并集,集合右边为所有可能出现的集合的交集,组合为新集合的集合个数为奇数的为正,否则为负。

#include<bits/stdc++.h>
using namespace std;
#define LL long long
LL gcd(LL a,LL b){return b==0?a:gcd(b,a%b);}
LL lcm(LL a,LL b){return a/gcd(a,b)*b;}
int main()
{
LL n;
while(cin>>n){LL ans=0;
ans=n/2+n/3+n/5+n/7-n/6-n/10-n/14-n/15-n/21-n/35+n/30
+n/42+n/70+n/105-n/210;
cout<<n-ans<<endl;

}
return 0;
}

posted @ 2017-05-21 22:07  *zzq  阅读(176)  评论(0编辑  收藏  举报