2018-hdu6286-容斥定理

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6286

Problem Description
Given a,b,c,d,find out the number of pairs of integers (x,y) where axb,cyd and xyis 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 

1ab10^9,1cd10^9 
* The number of tests cases does not exceed 10^4
Sample Input

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

Sample Output

3
6051
1485883320325200

转载自:https://blog.csdn.net/zzti_xiaowei/article/details/80379427

2018的因子有:1,2,1009,2018,分类讨论即可:
1. [a,b]中2018的倍数,[c,d]为任意数
2. [c,d]中2018的倍数,[a,b]为任意数
3. [a,b]中2018的倍数且[c,d]中2018的倍数(为了1,2情况去重)
4. [a,b]中1009的奇数倍的个数(偶数倍同1有重叠),[d,c]中2的倍数且不是2018的倍数
5. [c,d]中1009的奇数倍的个数(偶数倍同2有重叠),[a,b]中2的倍数且不是2018的倍数

#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
ll a,b,c,d;
ll f(ll x)//求出1-x中有多少个数是1009的奇数倍
{
    x/=1009;
    if(x%2)
        return x/2+1;
    return x/2;
}
int main()
{
    while(~scanf("%lld%lld%lld%lld",&a,&b,&c,&d)){
        ll sum=0;
        sum+=((b/2018)-(a-1)/2018)*(d-c+1);
        sum+=((d/2018)-(c-1)/2018)*(b-a+1);
        sum-=(b/2018-(a-1)/2018)*(d/2018-(c-1)/2018);
        sum+=(f(b)-f(a-1))*(d/2-(c-1)/2-(d/2018-(c-1)/2018));
        sum+=(f(d)-f(c-1))*(b/2-(a-1)/2-(b/2018-(a-1)/2018));
        printf("%lld\n",sum);
    }
    return 0;
}
posted @ 2019-07-17 15:45  里昂静  阅读(163)  评论(0编辑  收藏  举报