牛客14527 随机树

链接

点击跳转

题解

在DFS序序列上维护每种素数的幂次之和

代码

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 100010
#define maxe 100010
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define drep(i,a,b) for(i=a;i>=b;i--)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
    ll c, f(1);
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
    for(;isdigit(c);c=getchar())x=x*10+c-0x30;
    return f*x;
}
struct SegmentTree
{
    int sum[maxn<<2], L[maxn<<2], R[maxn<<2];
    void pushup(ll o)
    {
        sum[o]=sum[o<<1]+sum[o<<1|1];
    }
    void build(ll o, ll l, ll r)
    {
        ll mid(l+r>>1);
        L[o]=l, R[o]=r;
        if(l==r)return;
        build(o<<1,l,mid);
        build(o<<1|1,mid+1,r);
        pushup(o);
    }
    ll Sum(ll o, ll l, ll r)
    {
        ll mid(L[o]+R[o]>>1), ans(0);
        if(l<=L[o] and r>=R[o])return sum[o];
        if(l<=mid)ans+=Sum(o<<1,l,r);
        if(r>mid)ans+=Sum(o<<1|1,l,r);
        return ans;
    }
    void add(ll o, ll pos, ll v)
    {
        ll mid(L[o]+R[o]>>1);
        if(L[o]==R[o]){sum[o]+=v;return;}
        if(pos<=mid)add(o<<1,pos,v);
        else add(o<<1|1,pos,v);
        pushup(o);
    }
}segtree[6];
struct Graph
{
    int etot, head[maxn], to[maxe], next[maxe], w[maxe];
    void clear(int N)
    {
        for(int i=1;i<=N;i++)head[i]=0;
        etot=0;
    }
    void adde(int a, int b, int c=0){to[++etot]=b;w[etot]=c;next[etot]=head[a];head[a]=etot;}
    #define forp(_,__) for(auto p=__.head[_];p;p=__.next[p])
}G;
struct Easy_Tree
{
    int depth[maxn], dist[maxn], tid[maxn], rtid[maxn], tim, size[maxn], rev[maxn];
    void dfs(int pos, int pre, Graph& G)
    {
        tid[pos]=++tim;
        rev[tid[pos]]=pos;
        size[pos]=1;
        forp(pos,G)if(G.to[p]!=pre)
        {
            depth[G.to[p]]=depth[pos]+1;
            dist[G.to[p]]=dist[pos]+G.w[p];
            dfs(G.to[p],pos,G);
            size[pos]+=size[G.to[p]];
        }
        rtid[pos]=tim;
    }
    void run(Graph& G, int root)
    {
        tim=0;
        depth[root]=1;
        dfs(1,0,G);
    }
}et;
struct EasyMath
{
    ll prime[maxn], phi[maxn], mu[maxn];
    bool mark[maxn];
    ll fastpow(ll a, ll b, ll c)
    {
        ll t(a%c), ans(1ll);
        for(;b;b>>=1,t=t*t%c)if(b&1)ans=ans*t%c;
        return ans;
    }
    void exgcd(ll a, ll b, ll &x, ll &y)
    {
        if(!b){x=1,y=0;return;}
        ll xx, yy;
        exgcd(b,a%b,xx,yy);
        x=yy, y=xx-a/b*yy;
    }
    ll inv(ll x, ll p)  //p是素数
    {return fastpow(x%p,p-2,p);}
    ll inv2(ll a, ll p)
    {
        ll x, y;
        exgcd(a,p,x,y);
        return (x+p)%p;
    }
    void shai(ll N)
    {
        ll i, j;
        for(i=2;i<=N;i++)mark[i]=false;
        *prime=0;
        phi[1]=mu[1]=1;
        for(i=2;i<=N;i++)
        {
            if(!mark[i])prime[++*prime]=i, mu[i]=-1, phi[i]=i-1;
            for(j=1;j<=*prime and i*prime[j]<=N;j++)
            {
                mark[i*prime[j]]=true;
                if(i%prime[j]==0)
                {
                    phi[i*prime[j]]=phi[i]*prime[j];
                    break;
                }
                mu[i*prime[j]]=-mu[i];
                phi[i*prime[j]]=phi[i]*(prime[j]-1);
            }
        }
    }
    ll CRT(vector<ll> a, vector<ll> m) //要求模数两两互质
    {
        ll M=1, ans=0, n=a.size(), i;
        for(i=0;i<n;i++)M*=m[i];
        for(i=0;i<n;i++)(ans+=a[i]*(M/m[i])%M*inv2(M/m[i],m[i]))%=M;
        return ans;
    }
}em;
ll getcnt(ll n, ll p)
{
    ll ret=0;
    while(n%p==0)n/=p, ret++;
    return ret;
}
#define mod 1000000007ll
int main()
{
    ll n=read(), i, j;
    vector<ll> pr={2,3,5,7,11,13};
    rep(i,1,n-1)
    {
        ll u=read()+1, v=read()+1;
        G.adde(u,v);
    }
    et.run(G,1);
    rep(j,0,5)segtree[j].build(1,1,n);
    rep(i,1,n)
    {
        ll T=read();
        rep(j,0,5)
        {
            ll cnt = getcnt(T,pr[j]);
            segtree[j].add(1,et.tid[i],cnt);
        }
    }
    ll q=read();
    char s[10];
    while(q--)
    {
        scanf("%s",s);
        if(s[0]=='S')
        {
            ll u=read()+1, x=read();
            rep(j,0,5)
            {
                ll cnt = getcnt(x,pr[j]);
                segtree[j].add(1,et.tid[u],cnt);
            }
        }
        else
        {
            ll u=read()+1;
            ll ans=1, ans2=1;
            rep(j,0,5)
            {
                ll P = segtree[j].Sum(1,et.tid[u],et.rtid[u]);
                // printf("%lld^%lld\n",pr[j],P);
                ans *= em.fastpow(pr[j],P,mod);
                ans %= mod;
                ans2 *= P+1;
                ans2 %= mod;
            }
            printf("%lld %lld\n",ans,ans2);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值