开心的小Q 51Nod - 1742
开心的小Q
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 const ll maxn = 1e5+10; 5 ll mu[maxn], pri[maxn]; 6 ll cnt; 7 8 void init(){ 9 memset(pri, 0, sizeof(pri)); 10 cnt = 0; 11 mu[1] = 1; 12 for(ll i = 2; i < maxn; i++){ 13 if(!pri[i]) { 14 pri[cnt++] = i; 15 mu[i] = -1; 16 } 17 for(ll j = 0; j < cnt; j++){ 18 ll t = pri[j] * i; 19 if(t > maxn) break; 20 pri[t] = 1; 21 if(i % pri[j] == 0){ 22 mu[t] = 0; 23 break; 24 } else { 25 mu[t] = -mu[i]; 26 } 27 } 28 } 29 } 30 ll F(ll n){ 31 ll ans = n; 32 for(ll i = 1; i*i <= n; i++){ 33 ans -= mu[i]*(n/(i*i)); 34 } 35 return ans; 36 } 37 ll solve(ll a) { 38 ll ans = 0; 39 ll L = 1, R = 1; 40 while(L <= a) { 41 ans += (R-L+1)*F(a/L); 42 if(R >= a) break; 43 L = R+1; 44 R = a/(a/L); 45 } 46 return ans; 47 } 48 49 int main(){ 50 ll a,b; 51 init(); 52 while(scanf("%lld %lld", &a, &b) != EOF){ 53 printf("%lld\n", solve(b) - solve(a-1)); 54 } 55 }