Problem Statement

You are given positive integers $N$ and $M$.

Find the number, modulo $M$, of simple connected undirected graphs with $N$ vertices numbered $1, \dots, N$ that satisfy the following condition.

  • For every $u = 2, \dots, N-1$, the shortest distance from vertex $1$ to vertex $u$ is strictly smaller than the shortest distance from vertex $1$ to vertex $N$.

Here, the shortest distance from vertex $u$ to vertex $v$ is the minimum number of edges in a simple path connecting vertices $u$ and $v$.

Two graphs are considered different if and only if there are two vertices $u$ and $v$ that are connected by an edge in exactly one of those graphs.

Constraints

  • $3 \leq N \leq 500$
  • $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.


Sample Input 1

4 1000000000

Sample Output 1

8

Sample Input 2

3 100000000

Sample Output 2

1

Sample Input 3

500 987654321

Sample Output 3

610860515

Be sure to find the number modulo $M$.

首先有一种生成树叫做最短路树(在本题可以理解成BFS树)。其实就是一棵树,保证根节点到点 \(x\) 的距离等于原图中根节点到点 \(x\) 的距离。发现 \(n\) 一定是单独占据了一层。考虑在树上一层一层去dp.

定义 \(dp_{i,j}\) 为已经填了 \(i\) 个数,最后一层有 \(j\) 个数的方案数。为了保证 \(n\) 在最后一层,在dp过程中假设只有 \(n-1\) 个数,最后才把点 \(n\) 加上去。枚举上一层有多少个数,如果有 \(k\) 个,那么考虑从 \(dp_{i-j,k}\) 中转移。明显只有相邻的层之间可以互相连边,不然不能保证最短路树的性质。在这一层的 \(j\) 个数可以任意和上一层的 \(k\) 个数连边,但是不可以一个都不连,方案数 \((2^k-1)^j\)。从剩余的 \(n-1-i+j\) 个数中要选出 \(j\) 个数,方案有 \(C_{n-1-i+j}^j\) 种。同时同一层的点互相连是没有影响的,方案乘上 \(2^{j\times (j-1)}\)。总结,

\[dp_{i,j}=\sum\limits_{k=1}^{i-j}dp_{i-j,k}\times (2^k-1)^j\times C_{n-1-i+j}^j\times 2^{j\times (j-1)}
\]

最后统计答案时,当最后一层有 \(j\) 个,那么点 \(n\) 可以随便和这 \(j\) 个点连边,但也不能全部不连。所以答案为

\[\sum\limits_{j=1}^{n-1}dp_{n-1,j}\times(2^j-1)
\]

为了保证 \(O(1)\) 转移,中要预处理出 \(pw_{i,j}\) 表示 \((2^i-1)^j\) 以及组合数。

#include<cstdio>
const int N=505;
int n,P,dp[N][N],f[N][N],pw[N][N],pw2[N*N],ans;//f[i][j]表示选了i个数,最后一层选了j个的方案数
int main()
{
scanf("%d%d",&n,&P);
for(int i=pw2[0]=1;i<=n*n;i++)
pw2[i]=(pw2[i-1]<<1)%P;
for(int i=dp[0][0]=1;i<=n;i++)
{
dp[i][0]=dp[i][i]=1;
for(int j=1;j<i;j++)
dp[i][j]=(dp[i-1][j]+dp[i-1][j-1])%P;
}
for(int i=pw[0][0]=1;i<=n;i++)
{
int k=pw2[i]-1;
for(int j=0,p=1;j<=n;j++,p=1LL*p*k%P)
pw[i][j]=p;
}
f[1][1]=1;
// printf("%d\n",f[4][1]);
for(int i=2;i<n;i++)
{
// if(i==n)
// printf("%d\n",i);
for(int j=1;j<i;j++)
for(int k=1;k<=i-j;k++)
(f[i][j]+=1LL*f[i-j][k]*dp[n-1-i+j][j]%P*pw[k][j]%P*pw2[j*(j-1)/2]%P)%=P;
}
// printf("%d\n",n-1);
// for(int i=1;i<=n;i++)
// {
// printf("%d\n",i);
// for(int j=1;j<i;j++)
// printf("%d %d %d\n",i,j,f[i][j]);
// }
for(int j=1;j<n;j++)
(ans+=1LL*(pw2[j]-1)*f[n-1][j]%P)%=P;
printf("%d",ans);
}

[ABC281G] Farthest City的更多相关文章

  1. Gym101981D - 2018ACM-ICPC南京现场赛D题 Country Meow

    2018ACM-ICPC南京现场赛D题-Country Meow Problem D. Country Meow Input file: standard input Output file: sta ...

  2. BZOJ 2001: [Hnoi2010]City 城市建设

    2001: [Hnoi2010]City 城市建设 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 1132  Solved: 555[Submit][ ...

  3. History lives on in this distinguished Polish city II 2017/1/5

    原文 Some fresh air After your time underground,you can return to ground level or maybe even a little ...

  4. History lives on in this distinguished Polish city 2017/1/4

    原文 History lives on in this distinguished Polish city Though it may be ancient. KraKow, Poland, is a ...

  5. #1094 : Lost in the City

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi gets lost in the city. He does not know where he is ...

  6. GeoIP Legacy City数据库安装说明

    Here is a brief outline of the steps needed to install GeoIP Legacy City on Linux/Unix. The installa ...

  7. [POJ3277]City Horizon

    [POJ3277]City Horizon 试题描述 Farmer John has taken his cows on a trip to the city! As the sun sets, th ...

  8. 2015年第8本(英文第7本):the city of ember 微光城市

    书名:the City of Ember(中文名:微光城市) 作者:Jeanne DuPrau 单词数:6.2万 不重复单词数:未知 首万词不重复单词数:未知 蓝思值:未知 阅读时间:2015年4月2 ...

  9. 离散化+线段树 POJ 3277 City Horizon

    POJ 3277 City Horizon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18466 Accepted: 507 ...

  10. HDU 1505 City Game (hdu1506 dp二维加强版)

    F - City Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submi ...

随机推荐

  1. django配置swagger自动生成接口文档以及自定义参数设置

    首先安装swagger所用的包 pip install drf-yasg 然后再settings.py 中注册app 接口采用的token认证,在settings.py配置认证方式 SWAGGER_S ...

  2. Android13深入了解 Android 小窗口模式和窗口类型

    Android13深入了解 Android 小窗口模式和窗口类型 小窗模式,作为一种在移动设备上的多任务处理方式,为用户带来了便捷和高效的体验,尤其在一些特定场景下,其价值愈发凸显.以下是为什么需要小 ...

  3. 《Kali渗透基础》11. 无线渗透(一)

    @ 目录 1:无线技术 2:IEEE 802.11 标准 2.1:无线网络分层 2.2:IEEE 2.3:日常使用标准 2.3.1:802.11 2.3.2:802.11b 2.3.3:802.11a ...

  4. H.265+SRS6.0服务器部署

    H.265+SRS6.0服务器部署 SRS从6.0开始,全面支持H.265,包括RTMP.FLV.HLS.GB28181.WebRTC等等.具体的服务器部署及H.265推流步骤如下. 1. SRS 要 ...

  5. ATtiny88初体验(六):SPI

    ATtiny88初体验(六):SPI SPI介绍 ATtiny88自带SPI模块,可以实现数据的全双工三线同步传输.它支持主从两种模式,可以配置为LSB或者MSB优先传输,有7种可编程速率,支持从空闲 ...

  6. 《Python魔法大冒险》010 魔法宝箱:列表与元组的探险

    城堡的大门 随着小鱼和魔法师的深入,他们来到了一个古老的废弃城堡.城堡的大门上挂着一个巨大的锁,而锁的旁边有一排小抽屉,每个抽屉里都有一个物品. 魔法师对小鱼说:"这是一个古老的魔法宝箱,小 ...

  7. GORM自定义Gorm.Model实现自动添加时间戳

    废话不说直接开始 官网(http://gorm.io)有给出一套默认的gorm.Model模型,定义如下 package gorm import "time" // Model b ...

  8. 2.14 PE结构:地址之间的转换

    在可执行文件PE文件结构中,通常我们需要用到地址转换相关知识,PE文件针对地址的规范有三种,其中就包括了VA,RVA,FOA三种,这三种该地址之间的灵活转换也是非常有用的,本节将介绍这些地址范围如何通 ...

  9. mpi转以太网Plus模块连接300PLC实现MPI转modbus通信

    西门子200/300PLC转以太网同时实现PPI/MPI/DP转modbus通信 产品简介 MPI-ETH-XD1.0plus是在MPI-ETH-XD1.0的基础上,以太网口增加了支持与西门子带网口P ...

  10. 入门篇-其之四-字符串String的简单使用

    什么是字符串? 在Java编程语言中,字符串用于表示文本数据. 字符串(String)属于引用数据类型,根据String的源码,其头部使用class进行修饰,属于类,即引用数据类型. 字符串的表示 字 ...