牛客网Wannafly挑战赛27 A: 灰魔法师

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_41505957/article/details/83421596

链接:http://www.nowcoder.com/acm/contest/215/A
来源:牛客网
 

题目描述

“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) 两对。
#include<stdio.h>
#include<math.h>
#include<string.h>
#define N 110050
int a[2*N];
int main()
{
    int n,i,j,num;
	long long sum=0;
    while(scanf("%d",&n)!=EOF)
    {
    	sum=0;
	    for(i=0;i<n;i++)
	    {
	        scanf("%d",&num);
	        for(j=1;j<(int)sqrt(2*N);j++)
	        {
	        	if(j*j-num>0)
	        	{
	        		sum+=a[j*j-num];
	        		
				}		
			}
			a[num]++;
	    }
	    printf("%lld\n",sum);
	}
   
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41505957/article/details/83421596
今日推荐