2018(hdu 6286)

2018

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 507    Accepted Submission(s): 263

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

2018的因子只有1,2,1009,2018。那么我们可以对x进行分类讨论:

奇数且不能整除1009:  1 3 5 7 9 .........  1009 1009*3 1009*5....

偶数且不能整除2018:  2 4 6 8 .......... 2018 2018*2 2018*3.......

整除2018: 2018 2018*2 2018*3....

奇数且整除1009: 1009 1009*3 1009*5....

很明显,绿色部分重复了,我们需要去重处理一下。时间复杂度O(1)

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int main()
{
    LL a,b,c,d;
    while(scanf("%lld%lld%lld%lld",&a,&b,&c,&d)==4)
    {
        LL ans=0;
        LL x=0,y=0;
        x=(b/1009-(a-1)/1009);
        y=(b/2018-(a-1)/2018);
        x-=y;
        ans+=((b+1)/2-a/2-x)*(d/2018-(c-1)/2018);
        ans+=(b/2-(a-1)/2-y)*(d/1009-(c-1)/1009);
        ans+=y*(d-c+1);
        ans+=x*(d/2-(c-1)/2);
        printf("%lld\n",ans);
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值