加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

HDU - 6286

发布时间:2020-12-13 21:31:02 所属栏目:PHP教程 来源:网络整理
导读:题目链接: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? a ≤ x ≤ b , c ≤ y ≤ d ?and? x ? y ?is a multiple of? 2018. The input consists of several test ca

题目链接: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?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.

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("%lldn",sum);
26     }
27     return 0;
28 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读