# BZOJ 2301 莫比乌斯反演模板题 Problem b

#include <bits/stdc++.h>
#define FOR(i,s,t) for(int i=(s);i<=(t);i++)
#define ROF(i,s,t) for(int i=(s);i>=(t);i--)
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define fi first
#define se second
#define endl '\n'
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxn = 5e4 + 6;
const ll mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int x=0;
bool sign=false;
char c=getchar();
while(!isdigit(c)){
sign=c=='-';
c=getchar();
}
while(isdigit(c)){
x=x*10+c-'0';
c=getchar();
}
return sign?-x:x;
}
ll x=0;
bool sign=false;
char c=getchar();
while(!isdigit(c)){
sign=c=='-';
c=getchar();
}
while(isdigit(c)){
x=x*10+c-'0';
c=getchar();
}
return sign?-x:x;
}
string s;
char c=getchar();
while(isspace(c)){
c=getchar();
}
while(!isspace(c)){
s+=c;
c=getchar();
}
return s;
}

int mu[maxn+1], vis[maxn+1], summu[maxn+1];
vector <int> prime;

void getPrime(){
mu[1] = 1;
FOR(i,2,maxn){
if (!vis[i]) {
prime.pb(i);
mu[i] = -1;
}
for(int j = 0; j < prime.size() && i * prime[j] < maxn;j++){
vis[i * prime[j]] = 1;
if (i % prime[j] == 0) {
mu[i * prime[j]] = 0;
break;
}
mu[i * prime[j]] = - mu[i];
}
}
}

int a, b, c, d, k;

ll solve(ll n, ll m){
if (n > m) swap(n, m);
ll res = 0;int pos = 0;
for(int i = 1; i <= n; i = pos + 1){
pos = min(n/(n/i), m/(m/i));
res += (n/ i)* (m/i) * (summu[pos] - summu[i-1]);
}
return res;
}

int main(){
getPrime();
FOR(i,1,maxn){
summu[i] = summu[i-1] + mu[i];
}
while (T--){
ll ans = solve(b / k , d / k) + solve((a-1)/k, (c-1)/k) - solve((a-1)/k, d/k) - solve(b/k, (c-1)/k);
cout << ans << endl;
}

return 0;
}


©️2019 CSDN 皮肤主题: 创作都市 设计师: CSDN官方博客