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);
}

[ABC310G] Takahashi And Pass-The-Ball Game的更多相关文章

  1. [转] 如何应用设计模式设计你的足球引擎(三和四)----Design Football Game(Part III and IV)

    原文地址:http://www.codeproject.com/KB/cpp/applyingpatterns2.aspx 作者:An 'OOP' Madhusudanan 译者:赖勇浩(http:/ ...

  2. CodeChef CHEFSOC2 Chef and Big Soccer 水dp

    Chef and Big Soccer   Problem code: CHEFSOC2 Tweet     ALL SUBMISSIONS All submissions for this prob ...

  3. Python的并发并行[1] -> 线程[3] -> 多线程的同步控制

    多线程的控制方式 目录 唤醒单个线程等待 唤醒多个线程等待 条件函数等待 事件触发标志 函数延迟启动 设置线程障碍 1 唤醒单个线程等待 Condition类相当于一把高级的锁,可以进行一些复杂的线程 ...

  4. 【256】◀▶IEW-答案

    附答案 Unit I Fast food Model Answers: Model 1 The pie chart shows the fast foods that teenagers prefer ...

  5. codechef May Challenge 2016 CHSC: Che and ig Soccer dfs处理

    Description All submissions for this problem are available. Read problems statements in Mandarin Chi ...

  6. 【242】◀▶IEW-Unit07

    Unit 7 Education: Schools I.句子基本结构在写作中的运用 主谓宾 主系表 主谓 主谓宾宾 主谓宾补 1.主语: 1)位于句首 2)名词 例句:应该建立相关法律 Laws an ...

  7. 2019 GDUT Rating Contest II : Problem B. Hoofball

    题面: 传送门 B. Hoofball Input file: standard input Output file: standard output Time limit: 5 second Memor ...

  8. [模拟电路] 2、Passive Band Pass Filter

    note: Some articles are very good in http://www.electronics-tutorials.ws/,I share them in the Cnblog ...

  9. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' heade

    XMLHttpRequest cannot load http://10.164.153.37:8050/WebService/WebService.asmx/wsGetStreetData. Res ...

  10. UvaLA 3938 "Ray, Pass me the dishes!"

                            "Ray, Pass me the dishes!" Time Limit: 3000MS   Memory Limit: Unkn ...

随机推荐

  1. [ABC128D] equeue

    2023-01-14 题目 题目传送门 翻译 翻译 难度&重要性(1~10):4 题目来源 AtCoder 题目算法 暴力,贪心 解题思路 由题意可以得出,数据只有 \(n \leq 50,k ...

  2. 2023-08-22:请用go语言编写。给定一个长度为N的正数数组,还有一个正数K, 返回有多少子序列的最大公约数为K。 结果可能很大,对1000000007取模。 1 <= N <= 10^5, 1

    2023-08-22:请用go语言编写.给定一个长度为N的正数数组,还有一个正数K, 返回有多少子序列的最大公约数为K. 结果可能很大,对1000000007取模. 1 <= N <= 1 ...

  3. Git-入门使用资料

    一.Git入门教程 Git入门视频,针对于小白快速入门,时常约2~3小时 Git入门视频 相关课件资料: https://pan.baidu.com/s/1U-s4OmkToXJ5Y7BbJ7w2Ww ...

  4. CodeForces 1388C Uncle Bogdan and Country Happiness

    题意 给一棵\(n\)节点的树,每个节点有\(a[i]\)个人住,他们从\(1\)号节点回家,回家路上可能从开心的状态变成不开心的状态(但不可以由不开心变为开心),每个节点有个探测器,会探测经过该节点 ...

  5. ptp 时钟同步

    转载请注明出处: PTP(Precision Time Protocol)的功能可以帮助实现网络中各个节点的时钟同步,以提供更精确的时间参考. 作用: 时钟同步:通过PTP协议,在网络中不同节点之间实 ...

  6. 解密Prompt系列16. LLM对齐经验之数据越少越好?LTD & LIMA & AlpaGasus

    LLM Agent中间插个队,总结下指令微调.对齐数据相关的方案,已经凑够7篇论文可以召唤神龙啦!论文都是以优化指令样本为核心,Data-Centric的观点比较一致:指令微调也就是对齐阶段的数据质量 ...

  7. 其它——CGI,FastCGI,WSGI,uWSGI,uwsgi一文搞懂

    文章目录 CGI, FastCGI, WSGI, uWSGI, uwsgi一文搞懂 一 CGI 二 FastCGI 三 WSGI 四 uWSGI 五 uwsgi CGI, FastCGI, WSGI, ...

  8. 什么是DCloud

    什么是DCloud1.什么是Dcloud2.主要包括 1. 开发工具 2. 前端框架 3. uniCloud 4. 5+app 5. MUI 6. wap2app1.什么是Dcloud 1. Dclo ...

  9. 教育法学第七章单元测试MOOC

    第七章单元测试 返回 本次得分为:100.00/100.00, 本次测试的提交时间为:2020-09-06, 如果你认为本次测试成绩不理想,你可以选择 再做一次 . 1 单选(5分) 父母对未成年子女 ...

  10. 在线问诊 Python、FastAPI、Neo4j — 提供咨询接口服务

    目录 构建服务层 接口路由层 PostMan 调用 采用 Fast API 搭建服务接口: https://www.cnblogs.com/vipsoft/p/17684079.html Fast A ...