题目大意:
找出数组内 关于(i,j) ai-aj=i-j 的个数
题解:
进行变形 将 ai-aj=i-j 改为 ai-i=aj-j ,每次输入就进行 ai-i 并且储存相同数值的个数
例题可知
2个相同的数可以得出1种配对方式
3个相同的数可以得出3种配对方式
4个相同的数可以得出6种配对方式
5个相同的数可以得出10种配对方式
推出公式 n*(n-1)/2
#include <iostream>
#include <map>
using namespace std;
typedef long long int ll;
//n*(n-1)/2
int a[200005];
int main()
{
int t;
cin >> t;
while (t--)
{
map<int, int>mp;
int n;
cin >> n;
ll ans = 0;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
a[i] -= i;
mp[a[i]]++;
}
for (auto it : mp)
{
ll x = it.second;
ans += x * (x - 1) / 2;
}
cout << ans << endl;
}
}