题解:莫比乌斯反演+推公式
没看到a=c=1,写了a,c不恒为1也适用的情况。
#include"bits/stdc++.h"
#define debug(x) cout<<#x<<" = "<<x<<" "
using namespace std;
typedef long long LL;
const int MX = 1e5+17;
bool isprime[MX];
int prime[MX],mu[MX]= {0,1};
void mubi() {
memset(isprime,true, sizeof(isprime));
int primecount = 0;
for (int i = 2; i < MX; ++i) {
if (isprime[i]) {
prime[++primecount] = i;
mu[i]=-1;
}
for (int j = 1; i * prime[j] <MX; ++j) {
isprime[i * prime[j]] = false;
if(i%prime[j]==0) {
mu[i*prime[j]]=0;
break;
} else mu[i*prime[j]]=-mu[i];
}
}
}
void solve()
{
int a,b,c,d,k;
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
if(k == 0){
puts("0");
return;
}
a--; c--;
LL ans = 0;
int t;
t = min(b,d);
for(int j = k; j <= t; j+=k){
int x = min(b,d) - max(a,c);
ans += 1LL*mu[j/k]*(1LL*(b/j - a/j)*(d/j - c/j) - (1LL*(x/j)*(x/j) - (x/j))/2 );
}
cout<<ans<<endl;
}
int main()
{
#ifdef LOCAL
freopen("input.txt","r",stdin);
#endif // LOCAL
int T,cas = 0;
mubi();
for(scanf("%d",&T); T; T--){
printf("Case %d: ",++cas);
solve();
}
return 0;
}