1. 程式人生 > >開心的小Q 51Nod - 1742

開心的小Q 51Nod - 1742

span aps origin main const break else eof pre

開心的小Q

51Nod - 1742

交不上去,應該是可以過的吧~

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

開心的小Q 51Nod - 1742