CF2226B.Everything Everywhere

入门

通过率:0%

时间限制:1.50s

内存限制:256MB

AC君温馨提醒

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

题目描述

An array is called good if the difference between the maximum value and the minimum value in the array is equal to the greatest common divisor (GCD) of all the elements in the array. Note that an empty array is considered to be not good.

More formally, an array [a1,a2,…,am][a_1, a_2, \ldots, a_m] is good if and only if $$ \max(a_1, a_2, \ldots, a_m) - \min(a_1, a_2, \ldots, a_m) = \gcd(a_1, a_2, \ldots, a_m).$$

You are given a permutation∗^{\text{∗}} pp of length nn. Determine the number of good subarrays†^{\text{†}} in the given permutation.

∗^{\text{∗}}A permutation of length mm is an array consisting of mm distinct integers from 11 to mm in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array), and [1,3,4][1,3,4] is also not a permutation (m=3m=3 but there is 44 in the array).

†^{\text{†}}An array bb is a subarray of an array aa if bb can be obtained from aa by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. In particular, an array is a subarray of itself.

如果一个数组中最大值与最小值的差等于该数组所有元素的最大公约数(GCD),则称该数组为“好”数组。注意:空数组不被视为好数组。

更形式化地,数组 [a1,a2,…,am][a_1, a_2, \ldots, a_m] 是好数组,当且仅当

max⁡(a1,a2,…,am)−min⁡(a1,a2,…,am)=gcd⁡(a1,a2,…,am).\max(a_1, a_2, \ldots, a_m) - \min(a_1, a_2, \ldots, a_m) = \gcd(a_1, a_2, \ldots, a_m).

给定一个长度为 nn 的排列∗^{\text{∗}} pp,请确定该排列中“好”子数组†^{\text{†}} 的个数。

∗^{\text{∗}} 长度为 mm 的排列是指由 11 到 mm 这 mm 个互不相同的整数以任意顺序组成的数组。例如,[2,3,1,5,4][2,3,1,5,4] 是一个排列,而 [1,2,2][1,2,2] 不是排列(数字 22 在数组中出现了两次),[1,3,4][1,3,4] 也不是排列(此时 m=3m=3,但数组中却出现了 44)。

†^{\text{†}} 若数组 bb 可通过从数组 aa 的开头删除若干(可能为零或全部)元素、并从结尾删除若干(可能为零或全部)元素得到,则称 bb 是 aa 的一个子数组。特别地,一个数组是其自身的子数组。

输入格式

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 testcase contains a single integer nn (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5) — the length of the permutation pp.

The second line of each testcase contains nn integers p1,p2,…,pnp_1, p_2, \ldots, p_n (1≤pi≤n1 \le p_i \le n) — the permutation pp. It is guaranteed that pp is a permutation.

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

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

每个测试用例的第一行包含一个整数 nn(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5)—— 排列 pp 的长度。

每个测试用例的第二行包含 nn 个整数 p1,p2,…,pnp_1, p_2, \ldots, p_n(1≤pi≤n1 \le p_i \le n)—— 排列 pp。保证 pp 是一个排列。

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

输出格式

For each testcase, print a single integer — the number of good subarrays in the given permutation.

对于每个测试用例,输出一个整数——给定排列中“好”子数组的个数。

输入输出样例

  • 输入#1

    3
    2
    1 2
    9
    6 1 5 9 4 7 2 8 3
    4
    1 2 3 4

    输出#1

    1
    0
    3

说明/提示

For the first testcase, only one subarray is good, which is [1,2][1, 2].

For the second testcase, it can be proven that no good subarrays exist in the given permutation.

对于第一个测试用例,只有一个好子数组,即 [1,2][1, 2]。

对于第二个测试用例,可以证明在给定的排列中不存在好子数组。

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

首页