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

随机推荐

  1. 【opencv】传统目标检测:HOG+SVM实现行人检测

    传统目标分类器主要包括Viola Jones Detector.HOG Detector.DPM Detector,本文主要介绍HOG Detector与SVM分类器的组合实现行人检测. HOG(Hi ...

  2. 【pandas小技巧】--统计值作为新列

    这次介绍的小技巧不是统计,而是把统计结果作为新列和原来的数据放在一起.pandas的各种统计功能之前已经介绍了不少,但是每次都是统计结果归统计结果,原始数据归原始数据,没有把它们合并在一个数据集中来观 ...

  3. 文心一言 VS 讯飞星火 VS chatgpt (80)-- 算法导论7.4 5题

    五.如果用go语言,当输入数据已经"几乎有序"时,插入排序速度很快.在实际应用中,我们可以利用这一特点来提高快速排序的速度.当对一个长度小于 k 的子数组调用快速排序时,让它不做任 ...

  4. 论文解读(PERL)《PERL: Pivot-based Domain Adaptation for Pre-trained Deep Contextualized Embedding Models》

    Note:[ wechat:Y466551 | 可加勿骚扰,付费咨询 ] 论文信息 论文标题:PERL: Pivot-based Domain Adaptation for Pre-trained D ...

  5. 你能看到这个汉字么“  ” ?关于Unicode的私人使用区(PUA) 和浏览器端显示处理

    如果你现在使用的是chrome查看那么你是看不到我标题中的汉字的,显示为一个小方框,但是你使用edge查看的话,这个字就能正常的显示出来,不信你试试! 本故事源于我在做数据过程中遇到Unicode编码 ...

  6. 小白python和pycharm安装大佬勿扰

    编程语言发展和Python安装 计算机语言的发展 机器语言 1946年2月14日,世界上第一台计算机ENIAC诞生,使用的是最原始的穿孔卡片.这种卡片上使用的语言是只有专家才能理解的语言,与人类语言差 ...

  7. 如何利用电商API接口来获取商品数据

    要利用电商API接口来获取商品数据,我们可以按照以下步骤实现: 确定电商平台和API接口 不同的电商平台提供不同的API接口,因此我们需要确定我们要获取商品数据的电商平台,并选择相应的API接口进行调 ...

  8. 学习JavaScript的路径

    学习JavaScript的路径可以按照以下步骤进行: 了解基本概念:首先学习JavaScript的基本概念,包括变量.数据类型.运算符.数组.对象.循环和条件语句等.可以通过阅读相关的教材.在线课程或 ...

  9. Solution Set -「NOI Online R1」

    NOI-Online-T1-序列 其实这道题是全场最难的-- 我这里给出一种并查集的做法. 首先我们把操作2中的 \(u\) 和 \(v\) 合并 对于操作1我们可以把他转化为操作2来做. 比如我们针 ...

  10. Solution -「洛谷 P5046」「YunoOI 2019 模拟赛」Yuno loves sqrt technology I

    Description Link. 无修改区间求逆序对. Solution 首先有一个显然的 \(\Theta(N\sqrt{N}\log_{2}N)\) 做法,由于过不了所以我就不废话. 其实有了 ...