51Nod-1284 2 3 5 7的倍数 容斥原理

题目链接:https://vjudge.net/problem/51Nod-1284
思路:

#include<iostream> 
#include<algorithm>
using namespace std;
int main()
{
    long long n;
    long long a,b,c,d;
          //  2 3 5 7
    long long ab,ac,ad,bc,bd,cd;
    long long abc,abd,acd,bcd;
    long long abcd;
    long long ans;
    while(cin>>n)
    {
        a=n/2,b=n/3,c=n/5,d=n/7;
        ab=n/6,ac=n/10,ad=n/14,bc=n/15,bd=n/21,cd=n/35;
        abc=n/30,abd=n/42,acd=n/70,bcd=n/105;
        abcd=n/210;
        ans=(a+b+c+d)-(ab+ac+ad+bc+bd+cd)+(abc+abd+acd+bcd)-abcd;
        cout<<n-ans<<endl;
    }
    return 0;
} 
Last modification:September 19th, 2019 at 12:24 am
如果觉得我的文章对你有用,请随意赞赏

Leave a Comment