0
点赞
收藏
分享

微信扫一扫

2018 容斥定理

浮游图灵 2022-03-13 阅读 69


2018

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2006    Accepted Submission(s): 941



Problem Description

Given , find out the number of pairs of integers  where  and  is a multiple of .



Input

The input consists of several test cases and is terminated by end-of-file.


Each test case contains four integers .



Output

For each test case, print an integer which denotes the result.


## Constraint


* The number of tests cases does not exceed .



Sample Input

1 2 1 2018 1 2018 1 2018 1 1000000000 1 1000000000



Sample Output

3 6051 1485883320325200

这题我怎么感觉我写过题解,无奈找不到了,重写一次吧

两个区间可以分成以下情况:

2018 容斥定理_java

由于2018的因子只有4个,于是采用手撸容斥定理写。答案就是

左边2的的部分乘右边1009的部分

加左边1009的的部分乘右边2的部分

加左边2018部分乘右边1部分加

加左边1部分乘右边2018部分加

减去左边2018乘右边2018

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a,b,c,d;
int main()
{
while(cin>>a>>b>>c>>d)
{
ll n2=b/2-(a-1)/2;
ll n1009=b/1009-(a-1)/1009;
ll n2018=b/2018-(a-1)/2018;
n2-=n2018;
n1009-=n2018;

ll m2=d/2-(c-1)/2;
ll m1009=d/1009-(c-1)/1009;
ll m2018=d/2018-(c-1)/2018;
m2-=m2018;
m1009-=m2018;
ll ans=n2*m1009+m2*n1009+n2018*(d-c+1)+m2018*(b-a+1)-n2018*m2018;
printf("%lld\n",ans);
}
}



举报

相关推荐

0 条评论