HDU-6286-2018(容斥原理)

2018

Problem Description

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.

Input

The input consists of several test cases and is terminated by end-of-file.

Each test case contains four integers a,b,c,d.

Output

For each test case, print an integer which denotes the result.

Constraint

  • 1≤a≤b≤109,1≤c≤d≤109
  • The number of tests cases does not exceed 104.

Sample Input

1 2 1 2018
1 2018 1 2018
1 1000000000 1 1000000000
 

Sample Output

3
6051
1485883320325200
 

Source
CCPC2018-湖南全国邀请赛-重现赛(感谢湘潭大学)

解题思路:

2018 可以拆分成 20181 12018 21009 10092
所以先数出两个区间内1,2,1009,2018,的倍数。
然后相乘,注意容斥。
2018是2的倍数,也是1009的倍数,所以乘2018的时候要减去1009和2的个数。

AC代码:

#include<iostream>
#include<string>
using namespace std;
typedef long long ll;
const int N = 1e6+10;
ll cnt1[2019],cnt2[2019];
ll sum1,sum2,sum3,sum4;
int main(){
	ll a,b,c,d;
	while(cin>>a>>b>>c>>d)
    {
        cnt1[1]    = b-a+1;         cnt2[1]    = d-c+1;
        cnt1[2]    = b/2 - (a-1)/2;     cnt2[2]    = d/2 - (c-1)/2;
        cnt1[1009] = b/1009 - (a-1)/1009;  cnt2[1009] = d/1009 - (c-1)/1009;
        cnt1[2018] = b/2018 - (a-1)/2018;  cnt2[2018] = d/2018 - (c-1)/2018;

        sum1 = cnt1[1]*cnt2[2018];
        sum2 = cnt1[2]*(cnt2[1009]-cnt2[2018]);
        sum3 = cnt1[1009]*(cnt2[2] - cnt2[2018]);
        sum4 = cnt1[2018]*(cnt2[1] - cnt2[2] - cnt2[1009] + cnt2[2018]);
        cout<<sum1+sum2+sum3+sum4<<endl;
    }
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值