灰魔法师

Links: nowcoder-215-B
HPUOJ-contest-3-B
Description:
“White shores, and beyond. A far green country under a swift sunrise.”--灰魔法师

给出长度为n的序列a, 求有多少对数对 (i, j) (1 <= i < j <= n) 满足 ai + aj 为完全平方数。
输入描述:
第一行一个整数 n (1 <= n <= 105)
第二行 n 个整数 ai (1 <= ai <= 105)
输出描述:
输出一个整数,表示满足上述条件的数对个数。
示例1
输入
3
1 3 6
输出
2
说明
满足条件的有 (1, 2), (2, 3) 两对。

Intentional analysis:
We should use a tag array because the data range is too large and the violence will time out.For this way,this problem is easy too.

Code:

#include<bits/stdc++.h>
using namespace std;
const int maxn=200050;
int a[maxn];
int main()
{
    int n,x,ans=0;
    cin>>n;
    while(n--)
    {
        cin>>x;
        for(int i=1;i*i<=maxn;i++)
            if(i*i>x)
                ans+=a[i*i-x];
        a[x]++;
    }
    cout<<ans<<endl;
    return 0;
}

The reason why I said this problem is meaningful is a thought,a "flag" thought,which can reduce the time the program runs to avoid TLE.