本文主要是介绍B. Pleasant Pairs,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
time limit per test
2 seconds
memory limit per test
256 megabytes
You are given an array a1,a2,…,ana1,a2,…,an consisting of nn distinct integers. Count the number of pairs of indices (i,j)(i,j) such that i<ji<j and ai⋅aj=i+jai⋅aj=i+j.
Input
The first line contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases. Then tt cases follow.
The first line of each test case contains one integer nn (2≤n≤1052≤n≤105) — the length of array aa.
The second line of each test case contains nn space separated integers a1,a2,…,ana1,a2,…,an (1≤ai≤2⋅n1≤ai≤2⋅n) — the array aa. It is guaranteed that all elements are distinct.
It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.
Output
For each test case, output the number of pairs of indices (i,j)(i,j) such that i<ji<j and ai⋅aj=i+jai⋅aj=i+j.
Example
Input
Copy
3 2 3 1 3 6 1 5 5 3 1 5 9 2
Output
Copy
1 1 3
Note
For the first test case, the only pair that satisfies the constraints is (1,2)(1,2), as a1⋅a2=1+2=3a1⋅a2=1+2=3
For the second test case, the only pair that satisfies the constraints is (2,3)(2,3).
For the third test case, the pairs that satisfy the constraints are (1,2)(1,2), (1,5)(1,5), and (2,3)(2,3).
解题说明:此题是一道数学题,遍历去找满足条件的组合,但找的时候注意没必要每次从头去找,第二个数找的时候直接找 arr[i] - i,同时每次跳过arr[i],否则题目会超时。
#include<stdio.h>
#include<stdlib.h>
int main()
{int t, n, i, j, count;long long arr[200000];scanf("%d", &t);while (t--){count = 0;scanf("%d", &n);for (i = 1; i <= n; i++){scanf("%lld", &arr[i]);}for (i = 1; i <= n; i++){for (j = arr[i] - i; j <= n; j = j + arr[i]){if (j > i && (i + j) == arr[i] * arr[j]){count++;}}}printf("%d\n", count);}return 0;
}
这篇关于B. Pleasant Pairs的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!