首页 > 其他分享 >[ABC310G] Takahashi And Pass-The-Ball Game

[ABC310G] Takahashi And Pass-The-Ball Game

时间:2023-07-22 14:12:53浏览次数:38  
标签:Ball int son Sample leq Game balls ABC310G Takahashi

Problem Statement

There are $N$ Takahashi.

The $i$-th Takahashi has an integer $A_i$ and $B_i$ balls.

An integer $x$ between $1$ and $K$, inclusive, will be chosen uniformly at random, and they will repeat the following operation $x$ times.

  • For every $i$, the $i$-th Takahashi gives all his balls to the $A_i$-th Takahashi.

Beware that all $N$ Takahashi simultaneously perform this operation.

For each $i=1,2,\ldots,N$, find the expected value, modulo $998244353$, of the number of balls the $i$-th Takahashi has at the end of the operations.

How to find a expected value modulo $998244353$

It can be proved that the sought probability is always a rational number. Additionally, the constraints of this problem guarantee that if the sought probability is represented as an irreducible fraction \(\frac{y}{x}\), then \(x\) is not divisible by \(998244353\).

Here, there is a unique \(0\leq z\lt998244353\) such that \(y\equiv xz\pmod{998244353}\), so report this \(z\).

Constraints

  • $1\leq N\leq 2\times10^5$
  • $1\leq K\leq 10^{18}$
  • $K$ is not a multiple of $998244353$.
  • $1\leq A _ i\leq N\ (1\leq i\leq N)$
  • $0\leq B _ i\lt998244353\ (1\leq i\leq N)$
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

$N$ $K$
$A _ 1$ $A _ 2$ $\cdots$ $A _ N$
$B _ 1$ $B _ 2$ $\cdots$ $B _ N$

Output

Print the expected value of the number of balls the $i$-th Takahashi has at the end of the operations for $i=1,2,\ldots,N$, separated by spaces, in a single line.


Sample Input 1

5 2
3 1 4 1 5
1 1 2 3 5

Sample Output 1

3 0 499122179 499122178 5

During two operations, the five Takahashi have the following number of balls.

If $x=1$ is chosen, the five Takahashi have $4,0,1,2,5$ balls.
If $x=2$ is chosen, the five Takahashi have $2,0,4,1,5$ balls.

Thus, the sought expected values are $3,0,\dfrac52,\dfrac32,5$. Print these values modulo $998244353$, that is, $3,0,499122179,499122178,5$, separated by spaces.


Sample Input 2

3 1000
1 1 1
1 10 100

Sample Output 2

111 0 0

After one or more operations, the first Takahashi gets all balls.


Sample Input 3

16 1000007
16 12 6 12 1 8 14 14 5 7 6 5 9 6 10 9
719092922 77021920 539975779 254719514 967592487 476893866 368936979 465399362 342544824 540338192 42663741 165480608 616996494 16552706 590788849 221462860

Sample Output 3

817852305 0 0 0 711863206 253280203 896552049 935714838 409506220 592088114 0 413190742 0 363914270 0 14254803

Sample Input 4

24 100000000007
19 10 19 15 1 20 13 15 8 23 22 16 19 22 2 20 12 19 17 20 16 8 23 6
944071276 364842194 5376942 671161415 477159272 339665353 176192797 2729865 676292280 249875565 259803120 103398285 466932147 775082441 720192643 535473742 263795756 898670859 476980306 12045411 620291602 593937486 761132791 746546443

Sample Output 4

918566373 436241503 0 0 0 455245534 0 356196743 0 906000633 0 268983266 21918337 0 733763572 173816039 754920403 0 273067118 205350062 0 566217111 80141532 0

期望是假的,其实题目是让 \(x\) 走 \(i(i\le k)\) 步后的点加上 \(a_i\)

容易发现是一棵基环树,环上树上分开考虑。

树上的点长剖计算就好,复杂度 \(O(n)\),关键是环上的点。

对每个树上的点,考虑他给环上的点带来的贡献,破环成链,差分计算即可。注意按照距离的不同,走的时候他有可能没走到。

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5,P=998244353;
typedef long long LL;
int n,q[N],l=1,r,a[N],b[N],e_num,hd[N],ans[N],inv,c[N<<1],in[N],s[N],dp[N],dfn[N],tme,son[N];
struct edge{
    int v,nxt;
}e[N];
LL k;
int read()
{
    char ch=getchar();
    int s=0;
    while(ch<'0'||ch>'9')
        ch=getchar();
    while(ch>='0'&&ch<='9')
        s=s*10+ch-48,ch=getchar();
    return s;
}
int pown(int x,int y)
{
    if(!y)
        return 1;
    int t=pown(x,y>>1);
    if(y&1)
        return 1LL*t*t%P*x%P;
    return 1LL*t*t%P;
}
void add_edge(int u,int v)
{
    e[++e_num]=(edge){v,hd[u]};
    hd[u]=e_num;
}
void doit(int x,int op)
{
    if(son[x])
        doit(son[x],1),ans[x]=(ans[son[x]]+b[son[x]])%P;
    for(int i=hd[x];i;i=e[i].nxt)
    {
        if(e[i].v==son[x])
            continue;
        doit(e[i].v,1);
        (ans[x]+=(ans[e[i].v]+b[e[i].v])%P)%=P;
        for(int j=0;j<dp[e[i].v];j++)
            (s[dfn[x]+j+1]+=s[dfn[e[i].v]+j])%=P;
    }
    if(k+1<dp[x])
        (ans[x]+=P-s[dfn[x]+k+1])%=P;
    if(!op)
        ans[x]=0;
}
void init(int x,int fa)
{
    s[dfn[x]=++tme]=b[x];
    if(son[x])
        init(son[x],x);
    for(int i=hd[x];i;i=e[i].nxt)
        if(e[i].v^son[x])
            init(e[i].v,x);
}
void dfs(int x,int dep,int fr)
{
    for(int i=hd[x];i;i=e[i].nxt)
        dfs(e[i].v,dep+1,fr);
    for(int i=hd[x];i;i=e[i].nxt)
        if(dp[e[i].v]>dp[son[x]])
            son[x]=e[i].v;
    dp[x]=dp[son[x]]+1;
    if(dep<=k)
    {
        LL p=(k-dep)%r,q=(k-dep)/r%P;
        (c[fr]+=1LL*(q+1)*b[x]%P)%=P;
        (c[fr+p+1]+=(P-b[x])%P)%=P;
        (c[fr+r]+=(P-1LL*q*b[x]%P)%P)%=P;
    }
}
void solve(int x)
{
    q[r=1]=x;
    int p=a[x];
    while(p^x)
    {
        q[++r]=p;
        p=a[p];
    }
    for(int i=1;i<=r;i++)
    {
        dfs(q[i],in[q[i]]=0,i);
        init(q[i],0);
        doit(q[i],0);
    }
    for(int i=1;i<=2*r;i++)
        (c[i]+=c[i-1])%=P,(ans[q[(i-1)%r+1]]+=c[i])%=P;
    for(int i=1;i<=r;i++)
        (ans[q[i]]+=(P-b[q[i]])%P)%=P;
    for(int i=1;i<=2*r;i++)
        c[i]=0;
}
int main()
{
    scanf("%d%lld",&n,&k),inv=pown(k%P,P-2);
    for(int i=1;i<=n;i++)
        a[i]=read(),in[a[i]]++;
    for(int i=1;i<=n;i++)
        b[i]=read();
    for(int i=1;i<=n;i++)
        if(!in[i])
            q[++r]=i;
    while(l<=r)
    {
        in[a[q[l]]]--;
        add_edge(a[q[l]],q[l]);
        if(!in[a[q[l]]])
            q[++r]=a[q[l]];
        l++;
    }
    for(int i=1;i<=n;i++)
        if(in[i])
            solve(i);
    for(int i=1;i<=n;i++)
        printf("%lld ",1LL*ans[i]*inv%P);
}

标签:Ball,int,son,Sample,leq,Game,balls,ABC310G,Takahashi
From: https://www.cnblogs.com/mekoszc/p/17573293.html

相关文章

  • AT_agc002_f [AGC002F] Leftmost Ball 思考--zhengjun
    思维+dp。如果像题意那样先放球再染色的话不是很好做。所以考虑有\(n\)个白球,\(n\)种其他颜色的球各\(k-1\)个。那么限制就是说对于每个前缀,白球的个数\(\ge\)其他颜色球的种数。所以就可以设\(f_{i,j}\)为放了\(i\)个白球,\(j\)种颜色的\(k-1\)个球的方案数。......
  • CF755G PolandBall and Many Other Balls
    列出转移方程就是傻鸟题了,具体地,令\(f_{i,j}\)为前\(i\)球取出\(j\)组的方案数,有:\[f_{i,j}=f_{i-1,j-1}+f_{i-1,j}+f_{i-2,j-1}\]列出\(f_{i}\)的GF\(F_i(x)\):\[F_i(x)=F_{i-1}(1+x)+F_{i-2}\cdotx\]这是递推,把矩阵元素改成多项式,矩阵快速幂即可。\(O(k\logk\log......
  • 数仓建模—Inmon范式建模与Kimball维度建模
    数仓建模—Inmon范式建模与Kimball维度建模在数据仓库领域,有两位大师,一位是“数据仓库”之父BillInmon,一位是数据仓库权威专家RalphKimball,两位大师每人都有一本经典著作,Inmon大师著作《数据仓库》及Kimball大师的《数仓工具箱》,两本书也代表了两种不同的数仓建设模式,这......
  • 103.Mr. Liang play Card Game
    杭电第一场补题103.Mr.LiangplayCardGame题目:有n张卡片,每一个卡片有自己的类型,等级、初始等级都是1。有以下两种操作:选择一张卡片打出去,获得权值为:val_{type_i}*p^{level-1}选择两个相邻,且相同种类,相同等级的卡片进行合并,合并之后等级+1.输出可以获得的最大权值......
  • 「解题报告」CF1067D Computer Game
    快国赛了,写点水题玩吧。首先容易有一个贪心策略:先以某种最优策略一直进行,直到成功一次后一直选择\(b_ip_i\)最大的进行。我们可以列出一个DP,设\(f_T\)表示在\(T\)时刻内期望最大收益,容易写出:\[f_T=\max\{p_i((T-1)v+a_i)+(1-p_i)f_{T-1}\}\]看起来就是可......
  • 【刷题笔记】55. Jump Game
    题目Givenanarrayofnon-negativeintegers,youareinitiallypositionedatthefirstindexofthearray.Eachelementinthearrayrepresentsyourmaximumjumplengthatthatposition.Determineifyouareabletoreachthelastindex.Example1:Input:......
  • poj 2311 Cutting Game (sg函数)
    小记:这题是对sg函数的初步理解。对于sg函数只要g[x]==0,则此时为必败态。x作为后继,我们就要对所有的后继进行标记,然后mex之。因为每次只能切一刀,所以切完之后,会有两块方格,而对每一块方格进行游戏又会有一个sg函数值,所以根据sg函数的性质,它这一刀所代表的后继,即为这两块方格的sg函......
  • Python pygame实现中国象棋单机版源码
    今天给大家带来的是关于Python实战的相关知识,文章围绕着用Pythonpygame实现中国象棋单机游戏版展开,文中有非常详细的代码示例,需要的朋友可以参考下#-*-coding:utf-8-*-"""CreatedonSunJun1315:41:562021@author:Administrator"""importpygamefrompygame.local......
  • Cutting Game
    题目来源:POJ2311CuttingGame题意给定一张\(N*M\)的矩形网格纸,两名玩家轮流行动。在每一次行动中,可以任选一张矩形网格纸,沿着某一行或者某一列的格线,把它剪成两部分。首先剪出\(1*1\)的玩家获胜。两名玩家都采取最优策略行动,求先手是否必胜。\[1\leqslantN,M\leqslant......
  • AtCoder Grand Contest 012 D Colorful Balls
    洛谷传送门AtCoder传送门不错的题。bxEnder32k。我们发现交换有传递性,那么我们能交换就连边,答案是\(\prod\frac{(sz)!}{\prodc_i!}\),其中\(sz\)为连通块大小,\(c_i\)为这个连通块中第\(i\)种颜色出现次数。于是我们得到了一个\(O(n^2)\)的做法。发现很多遍是无用的......