51Nod——N1284 2 3 5 7的倍数

https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1284

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

容斥原理
 1 #include <cstdio>
 2 
 3 #define LL long long
 4 
 5 using namespace std;
 6 
 7 LL n,tot;
 8 
 9 int main()
10 {
11     scanf("%lld",&n);
12     tot=(LL)n/2+n/3+n/5+n/7;
13     tot-=(LL)(n/6+n/10+n/14+n/15+n/21+n/35);
14     tot+=(LL)n/30+n/42+n/70+n/105;
15     tot-=(LL)(n/210);
16     printf("%lld",n-tot);
17     return 0;
18 }

 

 
posted @ 2017-07-09 16:33  Aptal丶  阅读(286)  评论(0编辑  收藏  举报