hdu 5552 Bus Routes

hdu 5552 Bus Routes

考虑有环的图不方便,可以考虑无环连通图的数量,然后用连通图的数量减去就好了。

无环连通图的个数就是树的个数,又 prufer 序我们知道是 $ n^{n-2} $ 其中又由于有 $ n-1 $ 个边,每个边可以涂色,所以总共无环的方案数量是 $ m^{n-1} n^{n-2} $

那么现在就要算连通图的数量了。这个不如不连通图的数量好算。

不连通图的数量怎么算呢,原本想的是容斥,但是貌似不好实现,看了题解发现一种神仙思路。考虑固定一个点,并且让这个点连出一个连通块,剩下的点随意连,必然不连通。并且由于最后图中一定有这个点,这样是可以不重不漏计算所有情况的。

考虑用 $ s(i) $ 表示 $ i $ 个点的的方案总数,就是 $ (m+1)^{\frac{n(n+1)}{2}} $ ,一共有 $ \frac{n(n+1)}{2} $ 个边,可以选择 $ m $ 种颜色的一种或者不要这个边。

考虑 $ f(i) $ 表示 $ i $ 个点的连通图的方案数。

$ f(n) = g(n) - \displaystyle\sum_{i=1}^n \binom{i-1}{n-1} f(i)g(n-i) $

这个看起来就很分治NTT

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define ll long long
using namespace std;
#define P 152076289
#define MAXN (1 << 19) + 13
int n , m;
int a[MAXN];
int Pow(int x,int y) {
    int res=1;
    while(y) {
        if(y&1) res=res*(ll)x%P;
        x=x*(ll)x%P,y>>=1;
    }
    return res;
}
int wn[2][MAXN];
void getwn(int l) {
    for(int i=1;i<(1<<l);i<<=1) {
        int w0=Pow(106,(P-1)/(i<<1)),w1=Pow(106,P-1-(P-1)/(i<<1));
        wn[0][i]=wn[1][i]=1;
        for(int j=1;j<i;++j)
            wn[0][i+j]=wn[0][i+j-1]*(ll)w0%P,
                    wn[1][i+j]=wn[1][i+j-1]*(ll)w1%P;
    }
}
int rev[MAXN];
void getr(int l) { for(int i=1;i<(1<<l);++i) rev[i]=(rev[i>>1]>>1)|((i&1)<<l-1); }
void NTT(int *A,int len,int f) {
    for(int i=0;i<len;++i) if(rev[i]<i) swap(A[i],A[rev[i]]);
    for(int l=1;l<len;l<<=1)
        for(int i=0;i<len;i+=(l<<1))
            for(int k=0;k<l;++k) {
                int t1=A[i+k],t2=A[i+l+k]*(ll)wn[f][l+k]%P;
                A[i+k]=(t1+t2)%P;
                A[i+l+k]=(t1-t2+P)%P;
            }
    if( f == 1 ) for(int inv=Pow(len,P-2),i=0;i<len;++i) A[i]=A[i]*(ll)inv%P;
}
int f[MAXN];
int A[MAXN] , B[MAXN];
int J[MAXN] , invJ[MAXN] , s[MAXN];
void CDQ(int *a,int *b,int l,int r){
    if( l == r ) { a[l] += s[l] , a[l] %= P; return; }
    int m = l + r >> 1;
    CDQ( a , b , l , m );
    int p = 1 , len = 0;
    while( p <= ( r - l + 1 ) * 2 ) p <<= 1 , ++ len;
    getr( len ) , getwn( len );
    for( int i = 0 ; i < p ; ++i ) A[i] = B[i] = 0;
    for( int i = l ; i <= m ; ++i ) A[i - l] = 1ll * a[i] * invJ[i - 1] % P;
    for( int i = 0 ; i <= r - l ; ++i ) B[i] = 1ll * s[i] * invJ[i] % P;
    NTT( A , p , 0 ) , NTT( B , p , 0 );
    for( int i = 0 ; i < p ; ++i ) A[i] = 1ll * A[i] * B[i] % P;
    NTT( A , p , 1 );
    for( int i = m + 1 ; i <= r ; ++i ) a[i] = ( a[i] - 1ll * J[i - 1] * A[i-l] % P + P ) % P;
    CDQ( a , b , m + 1 , r );
}
int kase = 0;
signed main() {
    J[0] = invJ[0] = 1;
    for( int i = 1 ; i < MAXN ; ++ i )
        J[i] = 1ll * J[i - 1] * i % P , invJ[i] = Pow( J[i] , P - 2 );
    int T;cin >> T;
    while( T --> 0 ) {
        cin >> n >> m; m %= P;
        memset( f , 0 , sizeof f ) , memset( s , 0 , sizeof s );
        for( int i = 1 ; i <= n ; ++ i )
            s[i] = Pow( m + 1 , 1ll * i * ( i - 1 ) / 2 % ( P - 1 ) );
        f[0] = 1;
        CDQ( f , a , 0 , n );
        int x;
        printf("Case #%d: %d\n",++ kase,( f[n] - 1ll * Pow(n, n - 2) * Pow(m, n - 1) % P + P) % P);
    }
}

原文地址:https://www.cnblogs.com/yijan/p/hdu5552.html

时间: 2024-10-17 12:06:09

hdu 5552 Bus Routes的相关文章

hdu 1690 Bus System (最短路径)

Bus System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6082    Accepted Submission(s): 1560 Problem Description Because of the huge population of China, public transportation is very importa

hdu 1690 Bus System(Floyd)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1690 Problem Description Because of the huge population of China, public transportation is very important. Bus is an important transportation method in traditional public transportation system. And it's

hdu 1960 Bus System

Bus System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6166    Accepted Submission(s): 1580 Problem Description Because of the huge population of China, public transportation is very importa

hdu 1690 Bus System(Dijkstra最短路)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1690 Bus System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6569    Accepted Submission(s): 1692 Problem Description Because of the huge popula

HDU 3420 -- Bus Fair ACM

Bus Fair Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 600    Accepted Submission(s): 293 Problem Description You are now in Foolish Land. Once moving in Foolish Land you found that there is

hdu 2377 Bus Pass

Bus Pass Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 667    Accepted Submission(s): 271 Problem Description You travel a lot by bus and the costs of all the seperate tickets are starting to

hdu 1690 Bus System(最短路)

问题: 链接:点击打开链接 题意: 思路: 代码: #include <iostream> #include <cstdio> #include <cstring> using namespace std; #define INF 1000000000000 typedef __int64 LL; const int N = 110; __int64 dis[N][N],place[N]; __int64 L1,L2,L3,L4,C1,C2,C3,C4; int n,m

815. Bus Routes

问题描述: We have a list of bus routes. Each routes[i] is a bus route that the i-th bus repeats forever. For example if routes[0] = [1, 5, 7], this means that the first bus (0-th indexed) travels in the sequence 1->5->7->1->5->7->1->... f

[LeetCode] 815. Bus Routes 巴士路线

We have a list of bus routes. Each routes[i] is a bus route that the i-th bus repeats forever. For example if routes[0] = [1, 5, 7], this means that the first bus (0-th indexed) travels in the sequence 1->5->7->1->5->7->1->... forever