CF2262C.Traveling the World

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There are nn islands, numbered from 11 to nn. The ii-th island has value aia_i. The values a1,a2,…,ana_1,a_2,\ldots,a_n are strictly increasing, and a1=0a_1=0.

Farmer John wants to rearrange the islands. After rearranging them, their values form an array b1,b2,…,bnb_1,b_2,\ldots,b_n, which is a rearrangement of aa.

Bessie then travels between islands according to the following rule. Suppose she is currently on the island in position ii after the rearrangement. Then Bessie may travel from the island in position ii to the island in position jj if $$ b_i + b_j = \max(b_i,b_{i+1},\ldots,b_n). $$

A rearrangement bb is called good if there exists a sequence of distinct positions p1,p2,…,pnp_1,p_2,\ldots,p_n such that Bessie can travel from pip_i to pi+1p_{i+1} for every 1≤i<n1\le i \lt n. In other words, Bessie can visit all positions (and therefore all islands) exactly once by following valid travels.

Count the number of good rearrangements of the islands. Since the number can be large, output it modulo 109+710^9+7.

有 nn 座岛屿,编号从 11 到 nn。第 ii 座岛屿的权值为 aia_i。权值序列 a1,a2,…,ana_1,a_2,\ldots,a_n 严格递增,且 a1=0a_1=0。

农夫约翰希望重新排列这些岛屿。重排后,它们的权值构成数组 b1,b2,…,bnb_1,b_2,\ldots,b_n,即 aa 的一个排列。

随后,奶牛贝茜按照如下规则在岛屿间移动:假设她当前位于重排后位置 ii 的岛屿上,则贝茜可以从位置 ii 的岛屿移动到位置 jj 的岛屿,当且仅当

b_i+b_j=max⁡(b_i,b_i+1,…,b_n).b\_i + b\_j = \max(b\_i,b\_{i+1},\ldots,b\_n).

若存在一个由互不相同的位置 p1,p2,…,pnp_1,p_2,\ldots,p_n 构成的序列,使得对每个 1≤i<n1\le i < n,贝茜均可从位置 pip_i 移动到位置 pi+1p_{i+1},则称该重排 bb 是好的。换言之,贝茜能恰好访问每个位置(从而访问每座岛屿)一次,且每次移动均满足上述规则。

请计算岛屿的好重排数量。由于答案可能很大,请输出其对 109+710^9+7 取模的结果。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains nn (4≤n≤2⋅1054 \le n \le 2 \cdot 10^5).

The second line of each test case contains nn distinct integers a1,a2,…,ana_1,a_2,\ldots,a_n (0≤ai≤1090\le a_i\le 10^9).

It is guaranteed that a1=0a_1=0 and a1<a2<⋯<ana_1 \lt a_2 \lt \cdots \lt a_n.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052\cdot 10^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(4≤n≤2⋅1054 \le n \le 2 \cdot 10^5)。

每个测试用例的第二行包含 nn 个互不相同的整数 a1,a2,…,ana_1,a_2,\ldots,a_n(0≤ai≤1090\le a_i\le 10^9)。

保证 a1=0a_1=0,且 a1<a2<⋯<ana_1 \lt a_2 \lt \cdots \lt a_n。

保证所有测试用例的 nn 之和不超过 2⋅1052\cdot 10^5。

输出格式

For each test case, output a single integer — the number of good rearrangements bb of aa, modulo 109+710^9+7.

对于每个测试用例,输出一个整数——数组 aa 的“好”重排 bb 的数量,对 109+710^9+7 取模。

输入输出样例

  • 输入#1

    3
    6
    0 1 2 3 4 5
    5
    0 3 4 5 6
    4
    0 1 3 4

    输出#1

    12
    0
    4

说明/提示

For the first testcase, one bb that works is [2,1,0,5,3,4][2,1,0,5,3,4].

Note that Bessie can travel from 1→51 \to 5 since 2+3=b1+b5=max⁡(b1,b2,…,b6)=52 + 3 = b_1 + b_5 = \max(b_1,b_2,\ldots,b_6) = 5.

Similarly, Bessie can travel from 5→25 \to 2, 2→62\to 6, 6→36\to 3, 3→43\to 4.

Thus, starting from island 11, Bessie can follow the path 1→5→2→6→3→41\to 5\to 2\to 6\to 3\to 4 which visits every island. Therefore, this rearrangement is good.

For the second testcase, it can be shown that there are no good rearrangements. Therefore, the answer is 00.

对于第一个测试用例,一个可行的 bb 是 [2,1,0,5,3,4][2,1,0,5,3,4]。

注意,贝茜可以从岛屿 11 到达岛屿 55,因为 2+3=b1+b5=max⁡(b1,b2,…,b6)=52 + 3 = b_1 + b_5 = \max(b_1,b_2,\ldots,b_6) = 5。

类似地,贝茜还可以从 5→25 \to 2、2→62\to 6、6→36\to 3、3→43\to 4。

因此,从岛屿 11 出发,贝茜可以沿路径 1→5→2→6→3→41\to 5\to 2\to 6\to 3\to 4 行进,从而访问所有岛屿。故该重排是“好的”。

对于第二个测试用例,可以证明不存在任何“好的”重排。因此答案为 00。

输入解题思路,AI测评打分。不知道怎么写?

首页