HDU - 6286

时间:2019-08-30
本文章向大家介绍HDU - 6286,主要包括HDU - 6286使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6286

Given a,b,c,d, find out the number of pairs of integers (x,y) where axb,cyd and xy 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.

## Constraint

1ab109,1cd109
* The number of tests cases does not exceed 104.

正解:容斥原理:

因为2018的因子只有1、2、1009、2018,所以只需计算以下五种情况:

1. [a,b]中2018的倍数,[c,d]为任意数
2. [c,d]中2018的倍数,[a,b]为任意数
3. [a,b]中2018的倍数且[c,d]中2018的倍数(为了1,2情况去重)
4. [a,b]中1009的奇数倍(偶数倍同1有重叠),[d,c]中2的倍数且不是2018的倍数
5. [c,d]中1009的奇数倍(偶数倍同2有重叠),[a,b]中2的倍数且不是2018的倍数

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<vector>
 5 #include<queue>
 6 #include<algorithm>
 7 using namespace std;
 8 typedef long long ll;
 9 ll a,b,c,d;
10 ll f(ll x)
11 {
12     x/=1009;
13     if(x%2)return x/2+1;
14     return x/2;
15 }
16 int main()
17 {
18     while(~scanf("%lld%lld%lld%lld",&a,&b,&c,&d)){
19         ll sum=0;
20         sum+=(b/2018-(a-1)/2018)*(d-c+1);
21         sum+=(d/2018-(c-1)/2018)*(b-a+1);
22         sum-=(b/2018-(a-1)/2018)*(d/2018-(c-1)/2018);
23         sum+=(f(b)-f(a-1))*(d/2-(c-1)/2-(d/2018-(c-1)/2018));
24         sum+=(f(d)-f(c-1))*(b/2-(a-1)/2-(b/2018-(a-1)/2018));
25         printf("%lld\n",sum);
26     }
27     return 0;
28 }

原文地址:https://www.cnblogs.com/HouraisanKaguya/p/11436839.html