[2018 江苏省大学生程序设计大赛]JSCPC2018K. 2018 (测试数据范围有扩大)

Problem

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

• Qing Jiang felt that the original edition of the test cases is too easy, so he enlarged the scope of data. The current edition is:

• 1 ≤ a ≤ b ≤ 2³¹ - 1, 1 ≤ c ≤ d ≤ 2³¹ - 1

• The number of tests cases is around at 1·10⁵.

Sample Input

1 2 1 2018

1 2018 1 2018

1 1000000000 1 1000000000

Sample Output

3

6051

1485883320325200



/*先找1009的倍数有a个,和2018倍数的有b个,a*偶数,b*任何数,注意:偶数里面也包括2018的倍数,任意数也包括1009的倍数*/ 

#include<iostream>
using namespace std;
long long all2018(long long xx,long long yy);//用于求两个区间段类含有2018的倍数的数有多少个。 
long long all1009(long long xx,long long yy,long long all_of_2018);// 用于求两个区间段类含有1009的倍数的数有多少个。 
int main(){
long long a,b,c,d;
while(cin>>a>>b>>c>>d)
{
//x表示含有1009的倍数的数的个数。 
//x1表示含有2018的倍数的数的个数。
//y表示含有1009的倍数的数的个数。
//y1表示含有2018的倍数的数的个数。
//all1,all2表示含有偶数的个数。 
long long x1,y1,x,y,all1,all2,val;
x1=all2018(a,b);
y1=all2018(c,d);
x=all1009(a,b,x1);
y=all1009(c,d,y1);
all1=(b-a+1)/2;
all2=(d-c+1)/2;
if((b==a&&b%2==0)||(b%2==0&&a%2==0))
all1++;
if((c==d&&c%2==0)||(d%2==0&&c%2==0))
all2++;
val=x*all2+x1*(d-c+1)+y*all1+y1*(b-a+1)-x1*y1-y*x1-x*y1;
cout<<val<<endl;
}
return 0;
}
long long all2018(long long xx,long long yy)
{
long long all;
all=yy/2018-xx/2018;
if(xx>=2018&&yy>=2018)
{
if(xx%2018==0&&yy%2018==0)
all++;
else if(xx%2018==0&&yy%2018!=0)
all++;
}
return all;
}
long long all1009(long long xx,long long yy,long long all_of_2018)
{
long long all;
all=yy/1009-xx/1009;
if(yy>=1009&&xx>=1009)
{
if(xx%1009==0&&yy%1009==0)
all++;
else if(xx%1009==0&&yy%1009!=0)
all++;
}
return all-all_of_2018;
}

猜你喜欢

转载自blog.csdn.net/qq_40935723/article/details/80705307