题目 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;
}
}