https://codeforces.com/problemset/problem/1520/D
思路:将题目所问稍微变下形,a[ j ] - j = a[ i ] - i ,( i < j ),那么我们只需要用 map 存下每个数出现的次数然后再用组合公式就可以了
以下是代码实现:
#include<iostream>
#include<map>
using namespace std;
typedef long long ll;
const int N=2e5+10;
int a[N];
void solved()
{
map<int,ll>mp;
int n;
ll ans=0;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
a[i]-=i;
mp[a[i]]++;
}
for(auto i:mp)
{
if(i.second>=2)
{
ans+=(i.second*i.second-i.second)/2;
}
}
cout<<ans<<endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t;
cin>>t;
while(t--)
{
solved();
}
return 0;
}