初学---莫比乌斯反演

莫比乌斯反演是数论数学中很重要的内容,可以用于解决很多组合数学的问题。

1.莫比乌斯函数 u(d)

有如下定义:

 

有如下性质:

莫比乌斯打表

int mu[N+5];
vector<int> prim;
bool flag[N+5]={false};
void Mobius(){
    mu[1]=1;
    for(int i=2;i<N;i++){
        if(flag[i]==false){
            prim.push_back(i);
            mu[i]=-1;
        }
        for(int j=0;j<prim.size();j++){
            if(i*prim[j]>N) break;
            flag[i*prim[j]]=true;
            if(i%prim[j]==0){
                mu[i*prim[j]]=0;
                break;
            }
            else mu[i*prim[j]]=-mu[i];
        }
    }
//    for(int i=1;i<=10;i++) cout<<mu[i]<<endl;
}

 

 

 

2.莫比乌斯反演

 

3.反演技巧

重要的是找到f函数 ,通过与d,n以及f和F的关系确定F函数。

 

4.练习题

(1.HDU1695

Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y) = k. GCD(x, y) means the greatest common divisor of x and y. Since the number of choices may be very large, you're only required to output the total number of different number pairs. 
Please notice that, (x=5, y=7) and (x=7, y=5) are considered to be the same. 

Yoiu can assume that a = c = 1 in all test cases. 

InputThe input consists of several test cases. The first line of the input is the number of the cases. There are no more than 3,000 cases. 
Each case contains five integers: a, b, c, d, k, 0 < a <= b <= 100,000, 0 < c <= d <= 100,000, 0 <= k <= 100,000, as described above. 
OutputFor each test case, print the number of choices. Use the format in the example. 
Sample Input

2
1 3 1 5 1
1 11014 1 14409 9

Sample Output

Case 1: 9
Case 2: 736427

((1.gcd(x,y)=k可等价于gcd(x/k,y/k)=1.

((2.找到f(d)函数,设f(d)函数为gcd(x,y)==d的对数.

((3.确定公式,选择  (一般题都选用这个),F(n)为gcd(x,y)==n的对数,n为d的倍数,即F(n)为gcd(x,y)==d的倍数的对数.

((4.F函数可用数论整除求解,求1-20有多少是3的倍数,直接20/3==6,得到结果.

((5.ans1求解1-min(b,d),因为1-b,1-d中最大的gcd也只可能到b。而对于题意,如gcd(,a,b)和gcd(b,a)算一个,所以要再求一次1-b,1-b的情况。

 

cpp代码

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
const int N=1e5+5;
int mu[N+5];
vector<int> prim;
bool flag[N+5]={false};
void Mobius(){
    mu[1]=1;
    for(int i=2;i<N;i++){
        if(flag[i]==false){
            prim.push_back(i);
            mu[i]=-1;
        }
        for(int j=0;j<prim.size();j++){
            if(i*prim[j]>N) break;
            flag[i*prim[j]]=true;
            if(i%prim[j]==0){
                mu[i*prim[j]]=0;
                break;
            }
            else mu[i*prim[j]]=-mu[i];
        }
    }
//    for(int i=1;i<=10;i++) cout<<mu[i]<<endl;
}
int main(){
    Mobius();
    int t,a,b,c,d,k;
    cin>>t;
    for(int o=1;o<=t;o++){
        cin>>a>>b>>c>>d>>k;
        ll ans1=0,ans2=0;
        if(k==0) {
            cout<<"Case "<<o<<": "<<0<<endl;
            continue;
        }
        b/=k,d/=k;
        if(b>d) swap(b,d);
        for(ll i=1;i<=b;i++){
            ans1+=(ll)(b/i)*(d/i)*mu[i];
        }
        for(ll i=1;i<=b;i++){
            ans2+=(ll)(b/i)*(b/i)*mu[i];
        }
        cout<<"Case "<<o<<": "<<ans1-ans2/2<<endl;
    }
}

(2.HYSBZ 2818

给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的
数对(x,y)有多少对.

 

Input

一个整数N

Output

如题

Sample Input 4

Sample Output4Hint

 

hint

对于样例(2,2),(2,4),(3,3),(4,2)

与上题类似 ,不过在枚举i(F函数gcd值)的之前,还需要枚举素数,即f函数gcd(x,y)=素数,即为上题中的k。

附上学长的公式推导:

c++代码:

#include<iostream>
#include<vector>
using namespace std;
typedef long long ll;
const ll N=1e7+20;
vector<ll> prim;
int mu[N];
bool flag[N]={false};
void Mobius(){
    mu[1]=1;
    for(int i=2;i<N;i++){
        if(flag[i]==false){
            prim.push_back(i);
            mu[i]=-1;
        }
        for(int j=0;j<prim.size();j++){
            if(i*prim[j]>N) break;
            flag[i*prim[j]]=true;
            if(i%prim[j]==0){
                mu[i*prim[j]]=0;
                break;
            }
            else mu[i*prim[j]]=-mu[i];
        }
    }
//    for(int i=1;i<=10;i++) cout<<mu[i]<<endl;
}
int main(){
    Mobius();
    ll n,ans=0;
    cin>>n;
    for(ll i=0;prim[i]<=n;i++){
        ll tn=n/prim[i];
        for(int j=1;j<=tn;j++)
        ans+=(ll)(tn/j)*(tn/j)*mu[j];
    }
    cout<<ans<<endl;
}

 

转载于:https://www.cnblogs.com/jjl0229/p/11298833.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值