HDU - 6286 2018

Given a,b,c,d, find out the number of pairs of integers (x,y) where a≤x≤b,c≤y≤d and x⋅y is a multiple of 2018.

题解:容斥定理,分情况讨论
1.当x是2018的倍数时,y是任意值
2.当x是1009的倍数且要抛去2018的倍数的情况时,y是2的倍数
3.当x是2的倍数抛去2018的倍数时,y是1009的倍数
4.x其他情况,y是2008的倍数

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

int main()
{
    long long a,b,c,d;
    while(~scanf("%lld %lld %lld %lld",&a,&b,&c,&d)){
        long long ans = 0;
        long long x1,x2,x3,x4,q1,q2,q3,q4;
        // 如果x是2018的倍数,y是任意数
        x1 = b / 2018 - (a-1) / 2018; // [a,b] 中2018的倍数的个数
        q1 = d - c + 1;
        ans += x1 * q1;

        //如果x是1009的倍数不是2018的倍数,y是2的倍数
        x2 = b / 1009 - (a-1) / 1009 - x1;
        q2 = d / 2 - (c-1)/2;
        ans += x2 * q2;

        //如果x是2的倍数不是2018的倍数,y是1009的倍数
        x3 = b / 2 - (a - 1) / 2 - x1;
        q3 = d / 1009 - (c - 1) / 1009;
        ans += x3 * q3;

        //如果x是上面情况以外,y必须是2018的倍数
        x4 = b - a + 1 - x1 - x2 - x3;
        q4 = d / 2018 - (c - 1 ) /2018;
        ans += x4 * q4;

        cout <<ans <<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值