[ABC284G] Only Once
Problem Statement
For a sequence of length $N$, $A = (A_1,A_2,\dots,A_N)$, consisting of integers between $1$ and $N$, inclusive, and an integer $i\ (1\leq i \leq N)$, let us define a sequence of length $10^{100}$, $B_i=(B_{i,1},B_{i,2},\dots,B_{i,10^{100}})$, as follows.
- $B_{i,1}=i$.
- $B_{i,j+1}=A_{B_{i,j}}\ (1\leq j<10^{100})$.
Additionally, let us define $S_i$ as the number of distinct integers that occur exactly once in the sequence $B_i$.
More formally, $S_i$ is the number of values $k$ such that exactly one index $j\ (1\leq j\leq 10^{100})$ satisfies $B_{i,j}=k$.
You are given an integer $N$. There are $N^N$ sequences that can be $A$. Find the sum of $\displaystyle \sum_{i=1}^{N} S_i$ over all of them, modulo $M$.
Constraints
- $1\leq N \leq 2\times 10^5$
- $10^8\leq M \leq 10^9$
- $N$ and $M$ are integers.
Input
The input is given from Standard Input in the following format:
$N$ $M$
Output
Print the answer as an integer.
Sample Input 1
4 100000000
Sample Output 1
624
As an example, let us consider the case $A=(2,3,3,4)$.
- For $i=1$: we have $B_1=(1,2,3,3,3,\dots)$, where two integers, $1$ and $2$, occur exactly once, so $S_1=2$.
- For $i=2$: we have $B_2=(2,3,3,3,\dots)$, where one integer, $2$, occurs exactly once, so $S_2=1$.
- For $i=3$: we have $B_3=(3,3,3,\dots)$, where no integers occur exactly once, so $S_3=0$.
- For $i=4$: we have $B_4=(4,4,4,\dots)$, where no integers occur exactly once, so $S_4=0$.
Thus, we have $\displaystyle \sum_{i=1}^{N} S_i=2+1+0+0=3$.
If we similarly compute $\displaystyle \sum_{i=1}^{N} S_i$ for the other $255$ sequences, the sum of this value over all $256$ sequences is $624$.
Sample Input 2
7 1000000000
Sample Output 2
5817084
Sample Input 3
2023 998244353
Sample Output 3
737481389
Print the sum modulo $M$.
Sample Input 4
100000 353442899
Sample Output 4
271798911
题目讲的很复杂的样子。但是如果我们将 \(a_i\) 看作从 \(i\) 向 \(a_i\) 连一条边,那么将会连出来一棵基环树。题目要求的就是所有带标号基环树中所有非环上节点的深度和。
这题看似dp,却难以dp。考虑从数学角度入手。
枚举深度,尝试求出在所有的基环树中有多少个深度为 \(i\) 的节点。首先可以选出这个点到环上的 \(i\) 个节点组成的链,共 \(C_{n}^i\times i!\),然后选出这个环,枚举环上有 \(j\) 个节点,众所周知,一个 \(j\) 元环有 \((j-1)!\) 种排列方法,但是还要枚举这条链接到换上的那个节点,再乘个 \(j\),有 \(j!\) 种方法。剩下的点随便连,共 \(n^{n-i-j}\) 种。
最终答案为
\(\begin{aligned}
&\sum\limits_{i=1}^ni\sum\limits_{j=1}^{n-i}C_{n}^i\times i!\times C_{n-i}^j\times j!\times n^{n-i-j}\\&=\sum\limits_{i=1}^ni\sum\limits_{j=1}^{n-i}\frac{n!}{i!(n-i)!}\times i!\times \frac{(n-i)!}{j!(n-i-j)!}\times j!\times n^{n-i-j}\\&=\sum\limits_{i=1}^ni\sum\limits_{j=1}^{n-i}\frac{n!}{(n-i-j)!}\times n^{n-i-j}
\end{aligned}\)
会发现此时如果能调换一下求和顺序,会顺眼很多。但是后面即和 \(i\) 又和 \(j\) 有关,但是也之和 \(i+j\) 有关。令 \(T=i+j\)
\(\begin{aligned}
&=\sum\limits_{i=1}^ni\sum\limits_{j=1}^{n-i}\frac{n!}{(n-i-j)!}\times n^{n-i-j}\\&=\sum\limits_{T=2}^n\frac{n!}{(n-T)!}n^{n-T}\sum\limits_{i=1}^{T-1}i\\&=\sum\limits_{T=2}^n\frac{n!}{(n-T)!}n^{n-T}\frac 12 T(T-1)
\end{aligned}\)
#include<cstdio>
const int N=2e5+5;
int n,P,f[N],pw[N],ans;
int main()
{
scanf("%d%d",&n,&P);
for(int i=pw[0]=1;i<=n;i++)
pw[i]=pw[i-1]*1LL*n%P;
f[n+1]=1;
for(int i=n;i>=1;i--)
f[i]=f[i+1]*1LL*i%P;
for(int i=2;i<=n;i++)
(ans+=1LL*f[n-i+1]*pw[n-i]%P*(i*(i-1LL)/2%P)%P)%=P;
printf("%d",ans);
}
随机推荐
- RocketMQ系列(一) 基本介绍
RocketMQ系列(一) 基本介绍 1.MQ 作用 MQ 的应用场景主要包含以下 3 个方面: 1.1.异步与解耦 当我们下了一个订单之后,订单服务会进行 RPC 同步调用 支付服务.库存服务.物流 ...
- 如何get一个终身免费续期的定制数字人?
想拥有一个"数字分身" 吗?给你一个终身免费续期的特权. 定制周期长?训练.运营成本高?成片效果生硬?无法应用于实际场景? 随着AIGC技术的快速发展,虚拟数字人的生成效率不断提高 ...
- 图解Spark排序算子sortBy的核心源码
原创/朱季谦 一.案例说明 以前刚开始学习Spark的时候,在练习排序算子sortBy的时候,曾发现一个有趣的现象是,在使用排序算子sortBy后直接打印的话,发现打印的结果是乱序的,并没有出现完整排 ...
- xmind文件数据解析重构成mindmap可识别数据
[需求背景] 测试平台开发中,需要引入前端mindmap模块,进行在线xmind实时解析并前端展示 [卡点难点] 选取什么库进行xmind解析 如何转换成mindmap可以识别的数据 [xmind解析 ...
- 甲骨文宣布: 也做 PostgreSQL!
PostgreSQL 在开源数据库世界中一直是一个标志性的存在.经过35年的严格开发,它以其可靠性.强大的功能和性能而自豪.DB-engines 的突出显示,其市场份额的增长证明了其适应性强的数据模型 ...
- Sentinel系列之SlotChain、NodeSelectorSlot、ClusterBuilderSlot分析
本文基于Sentinel 1.8.6版本分析 1. SlotChain 我们从入口com.alibaba.csp.sentinel.SphU#entry(java.lang.String) 开始分析. ...
- 08-03_阅读flask上下文前夕补充、flask请求上下文、数据库连接池
文章目录 阅读flask上下文前夕补充 01 偏函数 02 __add__的使用 03 chain函数 2 flask请求上下文 1 首先分析请求上下文对象(ctx)创立 2 把请求对象(ctx)添加 ...
- Redis系列之——持久化
一 持久化的作用 1.1 什么是持久化 redis的所有数据保存在内存中,对数据的更新将异步的保存到硬盘上 1.2 持久化的实现方式 快照:某时某刻数据的一个完成备份, -mysql的Dump -re ...
- ida反汇编失败
options->general->打开堆栈指针(stack pointer) 可见反汇编失败提示的地址(本程序为00101481,原本此处不为000)处堆栈指针不为0,因此需要修改 al ...
- tomcat环境
tomcat环境部署时需要先部署JDK工具: JDK环境: #将上传的jdk包进行解压/并移至Java目录下: tar xf jdk1.8.0_131.tar.gz mkdir -p /usr/jav ...