江苏省赛 JSCPC2018 K. 2018

题目 K. 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.

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

题解
  • 给两个区间,分别在两个区间内选一个数x、y,满足x*y=2018
  • 问:存在多少对这样的数

代码

#include <cstdio>
#include <iostream>
using namespace std;

const int NUM[] = {1, 2, 1009, 2018};

void count(int n, int* cnt)
{
    cnt[3] = n / 2018;
    cnt[2] = n / 1009 - cnt[3];
    cnt[1] = n / 2 - cnt[3];
    cnt[0] = n - cnt[1] - cnt[2] - cnt[3];
}

long long solve(int n,  int m)
{
    static int a[4], b[4];
    count(n, a);
    count(m, b);
    long long result = 0;
    for (int i = 0; i < 4; ++ i) {
        for (int j = 0; j < 4; ++ j) {
            if (NUM[i] * NUM[j] % 2018 == 0) {
                result += 1LL * a[i] * b[j];
            }
        }
    }
    return result;
}

int main()
{
    int a, b, c, d;
    while (scanf("%d%d%d%d", &a, &b, &c, &d) == 4) {
        a --, c --;
        std::cout << solve(b, d) - solve(a, d) - solve(b, c) + solve(a, c) << std::endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值