2196B: Another Problem about Beautiful Pairs
brute force, math, two pointers, https://codeforces.com/problemset/problem/2196/B
【汤立祥 25物理学院 】思路:这道神秘的题目我一开始以为是在写一个
苦思冥想好几天之后点开Tutorial,发现给了一个非常巧妙的办法,就是利用
- 若
,有 ,不可能存在这样的对; - 若
,会被第一种方式搜到; - 若
,因此有 ,一定能被第二种方法搜到。 这样子整个算法就能在 的速度下完成计算。
这道题目的思路让我感觉是一种“Meet In Between"的思路。就是你有两种搜索方案,一共要完成
python
from math import sqrt, ceil
def solve():
n = int(input())
sqn = ceil(sqrt(n))
a = list(map(int, input().split()))
count = 0
for i in range(n):
current = a[i]
if current >= sqn:
for j in range(i % current, n, current):
if a[i] * a[j] == abs(j - i):
count += 1
else:
for j in range(i + current, min(i + sqn * current, n), current):
if a[i] * a[j] == j - i:
count += 1
print(count)
t = int(input())
for _ in range(t):
solve()