K



题意:给定两个区间 [a, b], [c, d],问有多少个有序对 (x, y)(x >= a && x <= b && y >= c && y <= d)使得,(x*y)= 0(mod 2018)。
输入:a, b, c, d。
数据范围:多组输入,所有输入均为[1, 1e9]。
The number of tests cases does not exceed 104104. 



/*
a b为左区间,c d为右区间 先求左区间中2018的个数,乘右区间的个数,同理求右区间 再求左区间中1009的倍数,但不是2018的倍数,再求右区间中2的倍数,但不是2018的倍数,同理右区间 再减去左区间和右区间中都是2018的倍数 */ #include<bits/stdc++.h> using namespace std; typedef unsigned long long ll; int main() { ll a,b,c,d; while(scanf("%llu%llu%llu%llu",&a,&b,&c,&d)==4) { ll tip1=a/2018,tip2=b/2018; ll x1=tip2-tip1; if(a%2018==0 ) x1++; tip1=c/2018,tip2=d/2018; ll x2=tip2-tip1; if(c%2018==0 ) x2++; tip1=a/1009,tip2=b/1009; ll y1=tip2-tip1; if(a%1009==0 ) y1++; y1-=x1; tip1=c/1009,tip2=d/1009; ll y2=tip2-tip1; if(c%1009==0 ) y2++; y2-=x2; tip1=a/2,tip2=b/2; ll z1=tip2-tip1; if(a%2==0) z1++; z1-=x1; tip1=c/2,tip2=d/2; ll z2=tip2-tip1; if(c%2==0 ) z2++; z2-=x2; // cout << x1 << " " << x2 << " " << y1 << " " << y2 << " " << z1 << " " << z2 << endl; ll ans=(b-a+1)*x2 + (d-c+1)*x1 + (y1*z2) + (y2*z1)-x1*x2; cout << ans << endl; } return 0; } 或者 #include<bits/stdc++.h> using namespace std; typedef long long LL; int main() { LL a,b,c,d; while(scanf("%lld%lld%lld%lld",&a,&b,&c,&d)==4) { LL ans=0; LL x=0,y=0; x=(b/1009-(a-1)/1009); y=(b/2018-(a-1)/2018); x-=y; ans+=((b+1)/2-a/2-x)*(d/2018-(c-1)/2018); ans+=(b/2-(a-1)/2-y)*(d/1009-(c-1)/1009); ans+=y*(d-c+1); ans+=x*(d/2-(c-1)/2); printf("%lld ",ans); } }
原文地址:https://www.cnblogs.com/zhangzhenjun/p/11609144.html