SCL--莫比乌斯反演

2016-04-19 19:08:55

莫比乌斯反演板子,推荐博客

 

 

关于 1 <= x <= a , 1 <= y <= b ,求 gcd(x,y)= 1 的对数的经典板子:

  注意:在 main 中调用 Mu_pre 预处理

 1 int mu[MAXN];
 2 int vis[MAXN],prime[MAXN],pcnt;
 3 int sum[MAXN];
 4 
 5 void Mu_pre(){
 6     mu[1] = 1;
 7     pcnt = 0;
 8     memset(vis,0,sizeof(vis));
 9     for(int i = 2; i < MAXN; ++i){
10         if(!vis[i]){
11             prime[++pcnt] = i;
12             mu[i] = -1;
13         }
14         for(int j = 1; j <= pcnt && i * prime[j] < MAXN; ++j){
15             vis[i * prime[j]] = 1;
16             if(i % prime[j]) mu[i * prime[j]] = -mu[i];
17             else{
18                 mu[i * prime[j]] = 0;
19                 break;
20             }
21         }
22     }
23     for(int i = 1; i < MAXN; ++i) sum[i] = sum[i - 1] + mu[i];
24 }
25 
26 ll Cal(int a,int b){
27     int top = min(a,b);
28     ll res = 0;
29     for(int i = 1; i <= top; ++i){
30         int j = min(a / (a / i),b / (b / i));
31         res += 1ll * (sum[j] - sum[i - 1]) * (a / i) * (b / i);
32         i = j;
33     }
34     return res;
35 }

 

posted @ 2016-04-19 19:12  Naturain  阅读(105)  评论(0编辑  收藏  举报