bzoj 2301 [HAOI2011]Problem b

#include <map>
#include <set>
#include <cmath>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;
typedef long long ll;
const int maxn = 5e5+10;

ll prime[maxn],mob[maxn],vis[maxn],sum[maxn],cnt;

void Mobius(){
memset(prime,0,sizeof(prime));
memset(mob,0,sizeof(mob));
memset(vis,0,sizeof(vis));
mob[1] = 1;
cnt = 0;
for(ll i = 2;i < maxn; i++){
if(!vis[i]){
prime[cnt++] = i;
mob[i] = -1;
}
for(ll j = 0;j < cnt&&i*prime[j] < maxn;j++){
vis[i*prime[j]] = 1;
if(i%prime[j]) mob[i*prime[j]] = -mob[i];
else{
mob[i*prime[j]] = 0;
break;
}
}
}
sum[0] = 0;
for(ll i = 1;i < maxn;i++) sum[i] = sum[i-1]+mob[i];
}

ll cal(ll n,ll m){
if(n > m) swap(n,m);
ll ans = 0,j;
for(ll i = 1;i <= n;i = j+1){
j = min(n/(n/i),m/(m/i));
ans += (sum[j]-sum[i-1])*(n/i)*(m/i);
}
return ans;
}

int main(){
Mobius();
ll T,a,b,c,d,k;
scanf("%lld",&T);
while(T--){
scanf("%lld%lld%lld%lld%lld",&a,&b,&c,&d,&k);
a--,c--;
a /= k,b /= k,c /= k,d /= k;
printf("%lld\n",cal(b,d)-cal(a,d)-cal(b,c)+cal(a,c));
}
return 0;
}

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客