#include<iostream> using namespace std; int a[1010]; void qsort(int a[],int l,int r) { int x=a[l],i=l,j=r; if(l>=r) return ; while(i<j) { while(i<j&&a[j]>=x) j--; a[i]=a[j]; while(i<j&&a[i]<=x) i++; a[j]=a[i]; } a[i]=x; qsort(a,l,i-1); qsort(a,i+1,r); } int main() { int n,i,m,ans,j,k; cin>>n; while(n--) { cin>>m; for(i=0;i<m;i++) cin>>a[i]; for(i=0;i<m;i++) a[i]=a[i]*a[i]; qsort(a,0,m-1); ans = 0 ; for(i = 0 ; i < m ; i++) { for(j = i+1 ; j < m ; j++) { k = a[i]+ a[j] ; int low = 0 , mid , high = m-1 ; while(low <= high) { mid = (low + high) / 2 ; if( a[mid] == k ) break; else if( a[mid] < k ) low = mid + 1 ; else high = mid - 1 ; } if(a[mid] == k) ans++ ; } } cout<<ans<<endl; } return 0; }
时间: 2024-10-05 04:05:26