一.题目内容
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).
二.解题思路
题意就是找出该数列中 i+j==a[i]*a[j]的个数 (i<j) 暴力两层for应该会超时 因为都是整数先确定a[i],
j是i的后一位 a[i]确定了 那么我们应该让i+j成为a[i]的倍数(i+j都不是a[i]的倍数 a[i]*a[j]之后一定不等于i+j,"假设i+j==a[i]*a[j] 那么i+j一定整除a[i] 为a[i]的倍数"),j正常为i+1 i+j=2*i+1 k=(2*i+1)/a[i]确定倍数再减去i 则是j首位 (确定首位i+j为a[i]的倍数,j>=i+1)因为i+j==k*a[i]之后j每次加a[i],使i+j始终为a[i]的倍数
三.代码
#include<bits/stdc++.h>
using namespace std;
long long a[100009];
int main()
{ long long n,m,num,t,k;
scanf("%lld",&m);
while (m--)
{ t=0,num=0;
scanf("%lld",&n);
for (int i = 1; i <=n ; ++i) {
scanf("%lld",&a[i]);
}
for (int i = 1; i <n ; ++i) { k=(2*i+1)/a[i];if ((2*i+1)%a[i]!=0)k++;
for (int j = k*a[i]-i; j <=n ; j+=a[i]) {
if (i+j==a[i]*a[j])t++;
}
}printf("%lld\n",t);
}
}