Count quadruplets ( A , B , C , D ) till N such that sum of square of A and B is equal to that of C and D | C ++ program for the above approach ; Function to count the quadruples ; Counter variable ; Map to store the sum of pair ( a ^ 2 + b ^ 2 ) ; Iterate till N ; Calculate a ^ 2 + b ^ 2 ; Increment the value in map ; Check if this sum was also in a ^ 2 + b ^ 2 ; Return the count ; Driver Code ; Given N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef long long int ll ; ll countQuadraples ( ll N ) { ll cnt = 0 ; map < ll , ll > m ; for ( ll a = 1 ; a <= N ; a ++ ) { for ( ll b = 1 ; b <= N ; b ++ ) { ll x = a * a + b * b ; m [ x ] += 1 ; } } for ( ll c = 1 ; c <= N ; c ++ ) { for ( ll d = 1 ; d <= N ; d ++ ) { ll x = c * c + d * d ; if ( m . find ( x ) != m . end ( ) ) cnt += m [ x ] ; } } return cnt ; } int main ( ) { ll N = 2 ; cout << countQuadraples ( N ) << endl ; return 0 ; }