HDU.6286.2018(容斥原理)

Given  a,b,c,da,b,c,d, find out the number of pairs of integers  (x,y)(x,y) where  axb,cyda≤x≤b,c≤y≤d and  xyx⋅y is a multiple of  20182018
InputThe input consists of several test cases and is terminated by end-of-file. 

Each test case contains four integers  a,b,c,da,b,c,d
OutputFor each test case, print an integer which denotes the result. 

## Constraint 

1ab109,1cd1091≤a≤b≤109,1≤c≤d≤109 
* The number of tests cases does not exceed  104104
Sample Input
1 2 1 2018
1 2018 1 2018
1 1000000000 1 1000000000
Sample Output
3
6051
1485883320325200


考点:容斥原理+基础数论

#include<iostream>
#include<cstdio>
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,ans1=b-a+1,ans2=d-c+1;
        long long k1=0,k2=0,k3=0,k4=0,k11=0,k22=0;

        k1=ans1/1009; if((((b-b%1009)-k1*1009)>=a)) k1++;
        k11=ans1/2018;if((((b-b%2018)-k11*2018)>=a)) k11++;
        k2=ans2/1009; if((((d-d%1009)-k2*1009)>=c)) k2++;
        k22=ans2/2018;if((((d-d%2018)-k22*2018)>=c)) k22++;

        k3=ans1/2;if((a%2==0)&&(b%2==0)) k3++;
        k4=ans2/2;if((c%2==0)&&(d%2==0)) k4++;
        //cout<<k1<<' '<<k2<<' '<<k3<<' '<<k4<<endl;
        //cout<<k11<<' '<<k22<<endl;

        if(!k11&&!k22) ans=k1*k4+k2*k3;
        else if(!k11&&k22) ans=k22*ans1+k4*k1+(k2-k22)*k3;
        else if(k11&&!k22) ans=k11*ans2+k1*k4+(k1-k11)*k4;
        else ans=k11*ans2+k22*ans1-k11*k22+(k1-k11)*(k4-k22)+(k2-k22)*(k3-k11);
        cout<<ans<<endl;
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值