时间限制: 1 Sec 内存限制: 128 MB
题目描述
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.
输入
The input consists of several test cases and is terminated by end-of-file.
Each test case contains four integers a, b, c, d
Each test case contains four integers a, b, c, d
输出
For each test case, print an integer which denotes the result.
• 1 ≤ a ≤ b ≤ 109 , 1 ≤ c ≤ d ≤ 109
• The number of tests cases does not exceed 104 .
• 1 ≤ a ≤ b ≤ 109 , 1 ≤ c ≤ d ≤ 109
• The number of tests cases does not exceed 104 .
样例输入
1 2 1 2018
1 2018 1 2018
1 1000000000 1 1000000000
样例输出
3
6051
1485883320325200
提示
来源
题解:
这道题的思路虽说很简单就是找在两个范围中分别取两个点并使他们的积是2018的倍数,重要的是要理解最后消去重复点的方法。
我们可以通过题目知道只要寻找两个范围中1009和2018的个数了,因为2018的约数只有1,2,1009,2018。一个范围中1009的个数乘以另一个范围中偶数的个数即是一部分2018的倍数的个数,还有种情况是一个范围中2018的个数乘以另一个范围中数字的个数即使一部分2018的倍数的个数,用同样的方法求另一个范围中的2018的倍数然后再加起来。
这样算出来可能有重复解,1009×2018,2018×2018,2018×1009.
AC代码
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <stdlib.h>
#include <list>
#include <map>
#include <utility>
#include <set>
#include <bitset>
#include <vector>
#define pi acos(-1.0)
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3fLL
#define ms(a,b) memset(a,b,sizeof(a))
#define ll long long int
using namespace std;
struct node
{
int s;
int e;
}p[1005];
int cmp(node x,node y)
{
return x.e<y.e;
}
ll a,b,c,d;
ll fw(ll x,ll y)
{
if(x%2==0 && y%2==0)
return (y-x)/2+1;
else if(x%2!=0 && y%2!=0)
return (y-x)/2;
else
return (y-x+1)/2;
}
int main()
{
while(scanf("%lld%lld%lld%lld",&a,&b,&c,&d)!=EOF)
{
ll f1=fw(a,b);
ll f2=fw(c,d);
ll temp;
ll ans=0;
ll a18,a19;
a18=0;
a19=0;
a18=b/2018-(a-1)/2018;//要把两侧排除掉 因为他可能满足条件
ans+=a18*(d-c+1);
a19=b/1009-(a-1)/1009-a18;
ans+=a19*f2;
ll a28,a29;
a28=0;
a29=0;
a28=d/2018-(c-1)/2018;
ans+=a28*(b-a+1);
a29=d/1009-(c-1)/1009-a28;
ans+=a29*f1;
printf("%lld\n",ans-(a19*a28+a18*a28+a29*a18));
}
return 0;
}