Majestic Pairs#12265

Majestic Pairs (Editorial)

Problem Author:

De3b0o

Prepared by:

De3b0o

Edited by:

Hasan Saleh (MathModel)

Solution

For a pair () to be majestic, it must satisfy the following condition:

  • a_i a_j

A pair () where (i,j \ge 1000) can't be majestic because then i \cdot j \ge 10^6 \ge a_i + a_j.

So we can for each , check if the pairs () (1 \le i \le \min(j,1000)) are majestic.

Complexity \mathcal{O}(1000 \cdot \sum(n)).


#include<bits/stdc++.h>
#define ll long long
#define d3 ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
 
using namespace std;

const ll N = 1e5+9;

ll n,ans;
ll a[N];

int main()
{
    d3
    ll T;
    cin >> T;
    while(T--)
    {
        cin >> n;
        for(int i = 1 ; n>=i ; i++)
            cin >> a[i];
        ans=0;
        for(int i = 1 ; n>=i ; i++)
        {
            for(int j = 1 ; min(i,1000)>=j ; j++)
            {
                if(a[i]+a[j]==i*j)
                    ans++;
            }
        }
        cout << ans << "\n";
    }
}