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?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. For each test case,print an integer which denotes the result. 正解:容斥原理: 因为2018的因子只有1、2、1009、2018,所以只需计算以下五种情况: 1. [a,b]中2018的倍数,[c,d]为任意数 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 } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |