forked from sureshmangs/Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1541B - Pleasant Pairs.cpp
75 lines (50 loc) · 1.5 KB
/
1541B - Pleasant Pairs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
You are given an array a1,a2,…,an consisting of n distinct integers. Count the number of pairs of indices (i,j) such that i<j and ai·aj=i+j.
Input
The first line contains one integer t (1=t=104) — the number of test cases. Then t cases follow.
The first line of each test case contains one integer n (2=n=105) — the length of array a.
The second line of each test case contains n space separated integers a1,a2,…,an (1=ai=2·n) — the array a. It is guaranteed that all elements are distinct.
It is guaranteed that the sum of n over all test cases does not exceed 2·105.
Output
For each test case, output the number of pairs of indices (i,j) such that i<j and ai·aj=i+j.
Example
inputCopy
3
2
3 1
3
6 1 5
5
3 1 5 9 2
outputCopy
1
1
3
Note
For the first test case, the only pair that satisfies the constraints is (1,2), as a1·a2=1+2=3
For the second test case, the only pair that satisfies the constraints is (2,3).
For the third test case, the pairs that satisfy the constraints are (1,2), (1,5), and (2,3).
*/
#include<bits/stdc++.h>
using namespace std;
void solve() {
long long n;
cin >> n;
vector <long long> v(n + 1);
for (int i = 1; i <= n; i++) cin >> v[i];
long long pairs = 0;
for (long long i = 1; i <= n; i++) {
for (long long j = v[i] - i; j <= n; j += v[i]) {
if ( j >= 0 && i < j && (v[i] * v[j] == i + j)) pairs++;
}
}
cout << pairs << "\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
cin >> t;
while (t--) solve();
return 0;
}