51nod 1284 2 3 5 7的倍数

给出一个数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

一开始想暴力打表,但是范围太大,看了解题思路是使用容斥原理。

讲解容斥原理的博客:http://www.cppblog.com/vici/archive/2011/09/05/155103.html
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <cmath>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
using namespace std;

const int INF=0x3f3f3f3f;
const int MX=500+10;

LL rongchi(LL x){
    LL ans=0;
    ans+=(x/2+x/3+x/5+x/7);
    ans-=(x/6+x/10+x/14+x/15+x/21+x/35);
    ans+=(x/30+x/42+x/70+x/105);
    ans-=(x/210);
    return ans;
}
int main(){
    LL n;
    while (~scanf ("%I64d",&n)) printf ("%I64d\n",n-rongchi(n));
    return 0;
}

 

posted @ 2016-08-15 20:37  Alex的ACM  阅读(174)  评论(0编辑  收藏  举报