洛谷 P3790 文艺数学题 & 洛谷 P4336 黑暗前的幻想乡(矩阵树定理+莫比乌斯反演/容斥原理)

题目传送门

https://www.luogu.org/problemnew/show/P3790

https://www.luogu.org/problemnew/show/P4336

文艺数学题

f(n) 为边权gcd为n的生成树个数,要求的是 Wn=1nf(n)

看到gcd就是反演。 f(n) 并不能直接求,我们设 F(n) 为边权gcd为n的倍数的生成树个数,即 F(n)=n|df(d) ,套路反演,有 f(n)=n|dμ(dn)F(d) ,这个式子复习一下证明,见下面(请跳过)

n|dμ(dn)F(d)=n|dμ(dn)d|xf(x)
d=sn,x=gd ,则 x=(gs)n ,化为
n|xf(x)s|xnμ(s)=n|xf(x)[x=n]=f(n)

然后求 Wn=1nn|dμ(dn)F(d)=Wd=1F(d)n|dnμ(dn)

=d=1WF(d)φ(d)

F(d) 可以用矩阵树定理去算,我们只加边权为d的倍数的边即可。理论上是过不了的,但是有用的d很少,首先d只能是某边权的约数,其次很多d都找不够n-1条边,所以可以预处理出满足每个d的边数,然后对于<=n-2条边的d直接跳过,这个优化很强,可以过了。

注意:自环要直接忽略,高斯消元的边界要处理好(越界调了一个晚上)。

#include 
#define maxn 66
#define maxm 3003
#define N 1000010
#define M 1000000007

using namespace std;

int A[maxn][maxn];

int n, m, cnt, ___;

int U[maxm], V[maxm], W[maxm], MW;
int f[N], prime[N], phi[N];
bool Vis[N];

int Read(){
    int x = 0;  char ch = getchar();
    while(ch < '0' || ch > '9')  ch = getchar();
    while(ch >= '0' && ch <= '9')  x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
    return x;
}

void Pre(){
    phi[1] = 1;
    for(int i = 2; i < N; i++){
        if(!Vis[i]){
            prime[++cnt] = i;
            phi[i] = i - 1; 
        }
        for(int j = 1; j <= cnt; j++){
            if(1LL * i * prime[j] >= 1LL * N)  break;
            Vis[i*prime[j]] = true;
            if(i % prime[j] == 0){
                phi[i*prime[j]] = phi[i] * prime[j];
                break;
            }
            phi[i*prime[j]] = phi[i] * (prime[j] - 1);
        }
    }
}

int Pow(int x, int y){
    int res = 1;
    for(; y; x = 1LL * x * x % M, y >>= 1)
        if(y & 1)  res = 1LL * res * x % M;
    return res;
} 

int Det(){

    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            A[i][j] = (A[i][j] + M) % M; 

    int rev = 0, ans = 1;
    for(int i = 2; i <= n; i++){
        int pos = i;
        for(; pos <= n && !A[pos][i]; pos++);

        if(pos != i)  rev ^= 1, swap(A[pos], A[i]);
        if(!A[i][i])  return 0;
        ans = 1LL * ans * A[i][i] % M;

        int Inv = Pow(A[i][i], M-2);
        for(int j = i+1; j <= n; j++){
            if(!A[j][i])  continue;
            int Mul = 1LL * A[j][i] * Inv % M;
            for(int k = 2; k <= n; k++)
                A[j][k] = (A[j][k] - 1LL * A[i][k] * Mul % M + M) % M;
        }
    }
    if(rev)  ans = (M - ans) % M;
    return ans;
}

int main(){

    Pre();

    n = Read();  m = Read();
    for(int i = 1; i <= m; i++)  U[i] = Read(), V[i] = Read(), W[i] = Read();

    for(int i = 1; i <= m; i++){
        for(int j = 1; j * j <= W[i]; j++){
            if(W[i] % j)  continue;
            f[j] ++;
            if(W[i] != j * j)  f[W[i] / j] ++;
        }
        MW = max(MW, W[i]);
    }


    for(int i = 1; i <= MW; i++){
        if(f[i] < n - 1)  continue;
        memset(A, 0, sizeof(A));

        for(int j = 1; j <= m; j++){
            if(U[j] == V[j] || W[j] % i)  continue; 
            A[U[j]][U[j]] ++;
            A[V[j]][V[j]] ++;
            A[U[j]][V[j]] --;
            A[V[j]][U[j]] --;
        }
        ___ = (___ + 1LL * phi[i] * Det() % M) % M; 
    }
    printf("%d\n", ___);

    return 0;
}

黑暗前的幻想乡

这题比上一题更水。
f(S) 表示 S 不取其他随便的方案, g(S) 表示只有 S 不取的方案。简单的子集反演:

f(S)=STg(T) g(S)=ST(1)|T||S|f(T)

要算 g(0) f 枚举集合然后矩阵树乱搞即可。不管怎样,跑得过。(是矩阵树定理的题都跑得过)

#include 
#define maxn 20
#define M 1000000007

using namespace std;
typedef long long LL;

int n, m[maxn];
vector <int> X[maxn], Y[maxn];

LL A[maxn][maxn], Violet;

LL Pow(LL x, int y){
    LL res = 1;
    for(; y; x = x * x % M, y >>= 1)
        if(y & 1)  res = res * x % M;
    return res;
}

LL Det(){
    int rev = 0;
    LL ans = 1;
    for(int i = 2; i <= n; i++){
        int x = i;
        for(; x <= n && !A[x][i]; x++);
        if(x != i)  rev ^= 1, swap(A[x], A[i]);

        if(!A[i][i])  return 0;
        ans = ans * A[i][i] % M;

        LL Inv = Pow(A[i][i], M-2);
        for(int j = i+1; j <= n; j++){
            if(!A[j][i])  continue;
            LL Mul = A[j][i] * Inv % M;
            for(int k = 2; k <= n; k++)
                A[j][k] = (A[j][k] - A[i][k] * Mul % M + M) % M;
        }
    }
    if(rev)  ans = (M - ans) % M;
    return ans;
}

int main(){

    scanf("%d", &n);

    int x, y;
    for(int i = 1; i < n; i++){
        scanf("%d", &m[i]);
        for(int j = 1; j <= m[i]; j++){
            scanf("%d%d", &x, &y);
            X[i].push_back(x), Y[i].push_back(y);
        }
    }

    for(int s = 0; s < (1<<(n-1))-1; s++){

        memset(A, 0, sizeof(A));

        int cnt = 0;
        for(int i = 1; i < n; i++){
            if((1<<(i-1)) & s){  
                cnt ++;
                continue;
            }
            for(int j = 0; j < m[i]; j++){
                A[X[i][j]][X[i][j]] ++;
                A[Y[i][j]][Y[i][j]] ++;
                A[X[i][j]][Y[i][j]] --;
                A[Y[i][j]][X[i][j]] --;
            }
        }

        if(cnt & 1)  Violet = (Violet - Det() + M) % M;
        else  Violet = (Violet + Det()) % M;
    }

    printf("%lld\n", Violet);

    return 0;
}

你可能感兴趣的:(数论,&,数学,莫比乌斯反演,&,杜教筛,容斥原理,高斯消元,矩阵树定理)