CF2176D.Fibonacci Paths
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a directed graph consisting of n vertices and m edges. Each vertex v corresponds to a positive number av. Count the number of distinct simple paths∗ consisting of at least two vertices, such that the sequence of numbers written at the vertices along the path forms a generalized Fibonacci sequence.
In this problem, we will consider that the sequence of numbers x0,x1,…,xk forms a generalized Fibonacci sequence if:
- x0,x1 are arbitrary natural numbers.
- xi=xi−2+xi−1 for all 2≤i≤k.
Note that a generalized Fibonacci sequence consists of at least two numbers.
Since the answer may be large, output it modulo 998244353.
∗A simple path in a directed graph is a sequence of vertices v1,v2,…,vk such that each vertex in the graph appears in the path at most once and there is a directed edge from vi to vi+1 for all i<k.
你被给定一个由 n 个顶点和 m 条有向边构成的有向图。每个顶点 v 对应一个正整数 av。请统计满足以下条件的不同简单路径∗ 的数量:该路径至少包含两个顶点,且路径上各顶点对应的数字序列构成一个广义斐波那契序列。
在本题中,我们称一个数字序列 x0,x1,…,xk 构成广义斐波那契序列,当且仅当:
- x0,x1 是任意正整数;
- 对所有 2≤i≤k,均有 xi=xi−2+xi−1。
注意:广义斐波那契序列至少包含两个数。
由于答案可能很大,请将结果对 998244353 取模后输出。
∗ 有向图中的一条简单路径是指一个顶点序列 v1,v2,…,vk,使得图中每个顶点在该路径中至多出现一次,且对所有 i<k,均存在一条从 vi 指向 vi+1 的有向边。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains two numbers n, m (2≤n≤2⋅105, 1≤m≤2⋅105) — the number of vertices and the number of edges in the graph, respectively.
The second line of each test case contains n natural numbers a1,a2,…,an (1≤ai≤1018) — the numbers written at the vertices.
The next m lines contain the edges of the graph; each edge is defined by two natural numbers v,u (1≤v,u≤n, u=v), denoting a directed edge from v to u. It is guaranteed that there are no multiple edges in the graph.
It is guaranteed that the sum of n and the sum of m across all test cases do not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含两个整数 n、m(2≤n≤2⋅105,1≤m≤2⋅105)——分别表示图中顶点数和边数。
每个测试用例的第二行包含 n 个正整数 a1,a2,…,an(1≤ai≤1018)——表示写在各顶点上的数值。
接下来的 m 行描述图中的边;每条边由两个正整数 v、u(1≤v,u≤n,u=v)定义,表示一条从顶点 v 指向顶点 u 的有向边。保证图中不存在重边。
保证所有测试用例的 n 之和与 m 之和均不超过 2⋅105。
输出格式
For each test case, output the number of paths that form a generalized Fibonacci sequence, modulo 998244353.
对于每个测试用例,输出构成广义斐波那契数列的路径数量,对 998244353 取模。
输入输出样例
输入#1
4 4 4 3 4 3 6 1 2 1 3 2 4 3 4 4 6 1 1 1 2 1 2 2 3 3 1 1 4 2 4 3 4 8 11 2 4 2 6 8 10 18 26 1 2 2 3 3 1 4 3 2 4 3 5 5 6 4 6 6 7 7 5 5 8 2 2 10 10 1 2 2 1
输出#1
5 9 24 2
说明/提示
Explanation of the first test case example (the vertex number is outside the brackets, the number written in the vertex is inside):

In this case, there are 5 generalized Fibonacci paths: (1,2), (1,3), (2,4), (3,4), (1,3,4). For example, for the path (1,3,4), the sequence of numbers written at the vertices along this path is: [3,3,6]. As can be easily seen, the third number in the sequence is the sum of the two previous ones.
Explanation of the second test case example:

In this case, there are 9 generalized Fibonacci paths: (1,2), (1,4), (2,3), (2,4), (3,1), (3,4), (1,2,4), (2,3,4), (3,1,4). Note that for the path (1,2,3), the sequence of numbers written at the vertices along this path is: [1,1,1], and it is not a generalized Fibonacci sequence.
第一个测试用例示例的说明(括号外为顶点编号,括号内为顶点上所写的数字):

本例中存在 5 条广义斐波那契路径:(1,2)、(1,3)、(2,4)、(3,4)、(1,3,4)。例如,对于路径 (1,3,4),沿该路径各顶点上所写数字构成的序列为:[3,3,6]。显然,该序列中第三个数等于前两个数之和。
第二个测试用例示例的说明:

本例中存在 9 条广义斐波那契路径:(1,2)、(1,4)、(2,3)、(2,4)、(3,1)、(3,4)、(1,2,4)、(2,3,4)、(3,1,4)。注意,对于路径 (1,2,3),沿该路径各顶点上所写数字构成的序列为:[1,1,1],它不构成广义斐波那契序列。
输入解题思路,AI测评打分。不知道怎么写?