[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);
}
随机推荐
- embed简介
go embed 是 Go 1.16 中引入的特性,它允许将文件嵌入到 Go 代码中,以便在运行时访问这些文件.这对于将静态资源(如 HTML.CSS.JavaScript 文件)直接嵌入到 Go 二 ...
- DevOps |研发效能之环境、程序、配置、SQL变更管理
本文主要是讲如何建立有效的环境.程序.配置.SQL变更和管理平台. 几天前和一个朋友聊到环境.程序的配置变更,SQL变更和整个上线流程.之前我们在这块也做了很多,有做的好的也有做的一般的,借机都总结 ...
- AnyLabeling标定及转化成labelmaskID
一.标定工具 在进行分割任务时,对分割工具进行预研和验证,现在AI辅助标定已经成熟,目标则是利用sam进行辅助标定.调研的三款标定工具情况如下: labelme:可以加载sam,但是在进行辅助标定后, ...
- 洛谷题解 | P1046 陶陶摘苹果
目录 题目描述 输入格式 输出格式 输入输出样例 说明/提示 题目思路 AC代码 题目描述 陶陶家的院子里有一棵苹果树,每到秋天树上就会结出 10 个苹果.苹果成熟的时候,陶陶就会跑去摘苹果.陶陶 ...
- go语言 包依赖管理-构建完整的依赖项目:目录结构及包的调用
目录结构: <home>/ |-- greetings/ |-- hello/1.分别进入对应目录创建以上目录结构 //bash切换到用户主目录 cd $HOMEPAHT$ //bash新 ...
- np.random.beta
numpy.random.beta(a,b,size=None) 从β分布中提取样本.β分布是狄里克莱分布的一个特例,与伽马分布有关. 在这里我们将参数(3个参数)设置为32 32 3 参数1:32次 ...
- Java 集合的排序(正序倒序)、查找元素的下边、最大值、最小值
Java 集合的排序(正序倒序).查找元素的下边.最大值.最小值 集合的排序 集合查找对应元素的下标 集合的最大最小值 集合的排序 使用Collections.sort()排序,默认是递增.加上比较器 ...
- redis主从同步及redis哨兵机制
1.主从和哨兵的作用: 角色 作用 主从 1.(提供)数据副本:多一份数据副本,保证redis高可用 2. 扩展(读)性能:如容量.QPS等 哨兵 1.监控: 监控redis主库及从库运行状态: 2 ...
- C++ 与 QML 之间进行数据交互的几种方法
一.属性绑定 这是最简单的方式,可以在QML中直接绑定C++ 对象的属性.通过在C++ 对象中使用Q_PROPERTY宏定义属性,然后在QML中使用绑定语法将属性与QML元素关联起来. person. ...
- 如何借助python第三方库存取不同应用程序的用户名、密码
在之前的一系列文章中,小爬分享了很多用Pywin32.uiAutomation.sap Gui Script等技术实现应用程序或者Web网站(如SAP.Excel.outLook邮件系统.OA系统)的 ...