CodeForces 630 K. Indivisibility(容斥)

Description
给出一整数n,问1~n中不能整除2~10中任意一个数的数的个数
Input
一整数n(1<=n<=10^18)
Output
输出1~n中不能整除2~10中任意一数的数的个数
Sample Input
12
Sample Output
2
Solution
能被4,6,8整除的都能被2整除,能被6,9整除的都能被3整除,能被10整除的都能被5整除,所以只要统计不能被2,3,5,7整除的数字即为答案,这个需要容斥一下,ans=n-[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]
Code

#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
ll n;
int main()
{
    while(~scanf("%I64d",&n))
    {
        ll ans=n-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;
        printf("%I64d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(CodeForces 630 K. Indivisibility(容斥))