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. [ABC151E] Max-Min Sums

    2023-03-11 题目 题目传送门 翻译 翻译 难度&重要性(1~10):5 题目来源 AtCoder 题目算法 数学 解题思路 对于一个正数 \(x,x\in A\) 一定会有 \(C_ ...

  2. shopee根据ID取商品详情 API

    ​ item_get-根据ID取商品详情  注册开通 shopee.item_get 公共参数 名称 类型 必须 描述 key String 是 调用key(必须以GET方式拼接在URL中) secr ...

  3. MATLAB入门小操作(数据类型)

    这是一篇有助于快速上手MATLAB软件的文章(新手向).(学习过其他的语言更容易看懂) 数据类型 这篇文章我想从MATLAB中的数据类型出发去列举一些经常使用的操作.MATLAB中的数据类型包括其他语 ...

  4. Andrew Ng 机器学习&深度学习课程 代码作业解答 集合

    写在最前 ​ 2018年是对自己来说是崭新的一年,在过去的3个多月里,从最基础的lr, 学到现在的LSTM, GAN..感觉第一次追上了计算机科学飞速发展的浪潮.虽然很多地方都仍是一知半解,但时间还长 ...

  5. 【项目源码】JavaWeb网上购书系统

    JavaWeb网上购书系统 介绍 采用JSP.Servlet.MySQL.Tomcat8.0开发的网上购书系统. 软件架构 网上书城主要功能如下: (1) 前台(客户购买)部分: ① 用户管理:注册会 ...

  6. Unity 性能优化之Shader分析处理函数ShaderUtil.HasProceduralInstancing: 深入解析与实用案例

    Unity 性能优化之Shader分析处理函数ShaderUtil.HasProceduralInstancing: 深入解析与实用案例 点击封面跳转到Unity国际版下载页面 简介 在Unity中, ...

  7. 从DevOps实践落地的角度谈谈“流程”和“规范"的反模式

    最近在经历的一些事情,让我突发灵感,觉得要写点关于DevOps体系建设过程中的"流程规范",记录下来. 如何解读"流程规范" 谈到DevOps落地,无一例外都会 ...

  8. salesforce零基础学习(一百三十一)Validation 一次的bypass设计

    本篇参考: https://admin.salesforce.com/blog/2022/how-i-solved-it-bypass-validation-rules-in-flows 背景:作为系 ...

  9. ORACLE DBLink创建

    在写测试脚本时,经常需要跨库取数据,SQL本身不支持跨库查找.Oracle提供DBLink链接,支持跨库操作. 1.创建DBLink Create public database link Next_ ...

  10. Python socket实现ftp文件下载服务

    简要 使用Python socket和多线程实现一个FTP服务下载.下面的示例是固定下载某一个任意格式文件. 仅仅为了展示如果使用socket和多线程进行文件下载 服务端代码 import socke ...