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

传送门

  • 题意:略

  • 解析:求多少数不是 2,3,5,7 的倍数,即求出多少数是 2,3,5,7 的倍数。2,3,5,7 本身互质,但由它们的倍数构成的集合*(6,10,14…)* 却有交集。于是就需要用上组合数学里的容斥原理
    AtAt

  • 延伸:DeMorgan定理
    At
    At

  • 代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <iomanip>
#include <bitset>
using namespace std;

#define eps         (1e-6)
#define LL          long long
#define pi          acos(-1.0)
#define ALL(a)      (a.begin(),(a.end())
#define ZERO(a)     memset(a,0,sizeof(a))
#define MINUS(a)    memset(a,0xff,sizeof(a))
#define IOS         cin.tie(0) , cout.sync_with_stdio(0)
#define PRINT(a,b)  cout << "#" << (a) << " " << (b) << endl
#define DEBUG(a,b)  cout << "$" << (a) << " " << (b) << endl
#define line        cout << "\n--------------------\n"

const LL INF = 1e18+100;
const int MAXN = 1e4 + 5;
const int MOD = 1e9+7;

LL n;
LL A[5];

void CONFIRM(LL a)
{
    PRINT("tot = ", a);
    line;
}

void INIT()
{
    A[1] = 2;
    A[2] = 3;
    A[3] = 5;
    A[4] = 7;
    return;
}

void SOLVE()
{
    LL tot = 0;
    INIT();
    
    for(int i=1; i<=4; ++i)
        tot += n / A[i];

    for(int i=1; i<=4; ++i)
        for(int j=i+1; j<=4; ++j)
            tot -= n / (A[i] * A[j]);

    for(int i=1; i<=4; ++i)
        for(int j=i+1; j<=4; ++j)
            for(int k=j+1; k<=4; ++k)
                tot += n / (A[i] * A[j] * A[k]);

    tot -= n / (A[1] * A[2] * A[3] * A[4]);

    cout << n - tot << endl;
    return;
}

int main()
{
    IOS;
    cin >> n;
    SOLVE();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值