码迷,mamicode.com
首页 > 其他好文 > 详细

51nod1284容斥定理

时间:2017-05-21 23:25:19      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:end   list   tps   html   ref   lis   公式   pid   lin   

基准时间限制: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;
}

51nod1284容斥定理

标签:end   list   tps   html   ref   lis   公式   pid   lin   

原文地址:http://www.cnblogs.com/zzqc/p/6886341.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!