[AGC005F] Many Easy Problems FFT Inclusion Principle

The main idea of the topic

Give you a tree with n points. And you have an integer k.

Let S be a set of points on a tree, and define f(S) as the smallest connected subgraph containing S.

In the k-point total (n k) scheme of N-point selection, please find out the f(S) and mod924844033 of all the schemes.

The author thinks it's too easy. He decides to ask you to find all k=1... The answer to n.

  n≤200000

Title Solution

It seems that there is no way to find the answer quickly for each k.

Let's consider the contribution of one point to all the answers.

A point x is in this connected subgraph if and when the k points are not in the subtree of X when x is the root.

Then the contribution is (nk)(aik), where ai is the size of each subtree when x is the root. It can be found that the subtree size at both ends of each side is calculated once when calculating the total contribution. (nk) is calculated n times.

Set up

bi={n          (i=n)numi   (i≠n)

Where numi is the number of subtrees of size i
ansk=∑i≥kbi(ik)=∑i≥kbii!(i−k)!k!=1k!∑i≥kbii!×1(i−k)!

This can be converted into convolution.
cn−idiaiansi=numii!=1i!=∑j+k=icidi=an−ii!

Time complexity: O(nlogn)

Code

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<ctime>
#include<utility>
#include<list>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const ll p=924844033;
const ll g=5;
ll fp(ll a,ll b)
{
    ll s=1;
    while(b)
    {
        if(b&1)
            s=s*a%p;
        a=a*a%p;
        b>>=1;
    }
    return s;
}
namespace ntt
{
    ll w1[1000010];
    ll w2[1000010];
    int rev[1000010];
    int n;
    void init()
    {
#ifdef DEBUG
        n=16;
#else
        n=524288;
#endif
        int i;
        for(i=1;i<=n;i<<=1)
        {
            w1[i]=fp(g,(p-1)/i);
            w2[i]=fp(w1[i],p-2);
        }
        rev[0]=0;
        for(i=1;i<n;i++)
            rev[i]=(rev[i>>1]>>1)|(i&1?n>>1:0);
    }
    void ntt(ll *a,int t)
    {
        int i,j,k;
        ll u,v,w,wn;
        for(i=0;i<n;i++)
            if(rev[i]<i)
                swap(a[i],a[rev[i]]);
        for(i=2;i<=n;i<<=1)
        {
            wn=(t==1?w1[i]:w2[i]);
            for(j=0;j<n;j+=i)
            {
                w=1;
                for(k=j;k<j+i/2;k++)
                {
                    u=a[k];
                    v=a[k+i/2]*w%p;
                    a[k]=(u+v)%p;
                    a[k+i/2]=(u-v)%p;
                    w=w*wn%p;
                }
            }
        }
        if(t==-1)
        {
            ll inv=fp(n,p-2);
            for(i=0;i<n;i++)
                a[i]=a[i]*inv%p;
        }
    }
};
ll b[1000010];
ll c[1000010];
ll a[1000010];
ll inv[1000010];
ll fac[1000010];
ll ifac[1000010];
int s[1000010];
int n;
list<int> l[200010];
ll num[1000010];
void dfs(int x,int fa)
{
    s[x]=1;
    for(auto v:l[x])
        if(v!=fa)
        {
            dfs(v,x);
            s[x]+=s[v];
            num[s[v]]--;
            num[n-s[v]]--;
        }
}
int main()
{
#ifdef DEBUG
    freopen("b.in","r",stdin);
    freopen("b.out","w",stdout);
#endif
    scanf("%d",&n);
    int i,x,y;
    for(i=1;i<=n-1;i++)
    {
        scanf("%d%d",&x,&y);
        l[x].push_back(y);
        l[y].push_back(x);
    }
    inv[0]=inv[1]=fac[0]=fac[1]=ifac[0]=ifac[1]=1;
    for(i=2;i<=n;i++)
    {
        inv[i]=-(p/i)*inv[p%i]%p;
        fac[i]=fac[i-1]*i%p;
        ifac[i]=ifac[i-1]*inv[i]%p;
    }
    dfs(1,0);
    num[n]=n;
    for(i=0;i<=n;i++)
        b[i]=num[n-i]*fac[n-i]%p;
    for(i=0;i<=n;i++)
        c[i]=ifac[i];
    ntt::init();
    ntt::ntt(b,1);
    ntt::ntt(c,1);
    for(i=0;i<ntt::n;i++)
        a[i]=b[i]*c[i]%p;
    ntt::ntt(a,-1);
    for(i=1;i<=n;i++)
    {
        ll ans=a[n-i]*ifac[i]%p;
        ans=(ans+p)%p;
        printf("%lld\n",ans);
    }
    return 0;
}

Posted by Craig79 on Wed, 13 Feb 2019 20:27:18 -0800