Given a, b, c, d, find out the number of pairs of integers (x, y) where a \leq x \leq b, c \leq y \leq da≤x≤b,c≤y≤d and x \cdot yx⋅y is a multiple of 2018. 输入描述: The input consists of several test cases and is terminated by end-of-file. Each test case contains four integers a, b, c, d. 输出描述: For each test case, print an integer which denotes the result. 示例1 输入 复制 1 2 1 2018 1 2018 1 2018 1 1000000000 1 1000000000 输出 复制 3 6051 1485883320325200 备注:
1 \leq a \leq b \leq 10^9, 1 \leq c \leq d \leq 10^91≤a≤b≤10 9 ,1≤c≤d≤10 9
The number of tests cases does not exceed 10^410 4 . 解题思路: 就是巧妙地统计下2018的倍数的个数以及1009的倍数的个数,再分别乘以偶数和所有数,最后再去重就OK了
AC代码:
#include<iostream> #include<bits/stdc++.h> using namespace std; long long int a2018(long long int x,long long int y) { long long int z; z=y/2018-x/2018; if(x>=2018&&y>=2018) { if(x%2018==0&&y%2018==0) { z++; } else if(x%2018==0&&y%2018!=0) { z++; } } return z; }; //求范围内2018的倍数的个数 long long int a1009(long long int x,long long int y,long long z) { long long int zz; zz=y/1009-x/1009; if(x>=1009&&y>=1009) { if(x%1009==0&&y%1009==0) { zz++; } else if(x%1009==0&&y%1009!=0) { zz++; } } return zz-z; }; //求范围内1009的倍数的个数 int main() { long long int a , b , c , d; while(cin >> a >> b >> c >> d) { long long int x1,y1,x2,y2,z1,z2,res; x1 = a2018(a,b); y1 = a2018(c,d); x2 = a1009(a,b,x1); y2 = a1009(c,d,y1); z1 = (b-a+1)/2; z2 = (d-c+1)/2; if((b==a&&b%2==0)||(b%2==0&&a%2==0)) z1++; if((c==d&&c%2==0)||(d%2==0&&c%2==0)) z2++; res = x2*z2+x1*(d-c+1)+y2*z1+y1*(b-a+1)-x1*y1-y2*x1-x2*y1; //这个地方只要手写一遍你就会明白 //就是1009乘偶数加2018乘任意数再减去重复加的部分 cout << res << endl; } return 0; }