QOJ.ac

QOJ

Time Limit: 3 s Memory Limit: 512 MB Total points: 100

#3274. 随机游走

统计

给定一张 $n$ 个点的有向图,点标号为 $1,2,\dots,n$,初始时对 $\forall i\in\{1,2,\dots,n-1\}$,从 $i$ 到 $i+1$ 有一条有向边。

你可以在其中再加入 $m$ 条有向边(起点终点任意),允许有重边和自环。

小 A 会从 $1$ 出发,以随机游走的形式行动,直到抵达 $n$。你希望最大化小A从 $1$ 移动到 $n$ 的期望步数。

定义随机游走是这样的一种移动方式:设小 A 当前在点 $x$,$x$ 有 $d$ 条出边,则小 A 会从这 $d$ 条出边中等概率随机选择一条走过去。

输入格式

从标准输入读入数据。

输入的第一行包含一个正整数 $T$,表示数据组数。

接下来 $T$ 行,每行包含三个整数 $n,m,p$,分别表示有向图的点数、你添加的边数以及答案的模数。

输出格式

输出到标准输出。

输出 $T$ 行,第 $i$ 行一个整数 $ans$ 表示第 $i$ 组数据中最大的期望步数对 $p$ 取模后的值(可以证明答案是有理数,设其用最简分数表示为 $\frac{a}{b}$,则你需要满足 $ans \cdot b \bmod p=a$,保证这样的 $ans$ 存在)。

样例数据

样例输入

4
3 2 97
10 25 233
6 12345 2333
1000000000 1000000000000000000 1000000007 

样例输出

6
131
1206
161905971

数据范围与提示

测试包编号 $n \le$ $m \le$ $T \le$ 特殊性质 分数
$1$ $5$ $5$ $10$ $10$
$2$ $10^{2}$ $10$
$3$ $10^{8}$ $10^{2}$ $20$
$4$ $50$ $3000$ $3$ $20$
$5$ $10^{9}$ $10^{9}$ $10^{5}$ $m\lt n-1$ $10$
$6$ $10^{18}$ 30

对于所有数据,保证 $T \le 10^5,1 \leq n \leq 10^9,0 \leq m \leq 10^{18},2\leq p\leq 10^9+7$ 且 $p$ 是质数。

About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.