📜  计算四元组(A,B,C,D)直到N,以使A和B的平方和等于C和D的平方和

📅  最后修改于: 2021-06-25 18:40:33             🧑  作者: Mango

给定数字N ,任务是找到四边形的数量,使得a 2 + b 2 = c 2 + d 2其中(1 <= a,b,c,d <= N)。

例子:

朴素的方法:朴素的方法是使用[1,N]范围内的4个嵌套循环并计算那些四倍体(a,b,c,d) ,使得a 2 + b 2 = c 2 + d 2

时间复杂度: O(N 4 )
辅助空间: O(1)

比朴素方法更有效的方法:使用某些数学方法,可以在时间复杂度方面减少上述解决方案。找出四元组,使a 2 + b 2 = c 2 + d 2

通过使用上面的公式,我们可以得出结论,只有(a 2 + b 2 – c 2 ) 1/2为正整数时d才存在。

时间复杂度: O(N 3 )

高效的方法:这个想法是使用Hashing 。步骤如下:

  1. 遍历[1,N]上的所有对(例如(a,b) ),并将a 2 + b 2的值及其出现位置存储在Map中。
  2. 遍历所有对[1,N] ,如果总和存在于映射中,则对存储在映射中的每对总和进行计数。
  3. 打印计数。

下面是该方法的实现:

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


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to count the quadruples
static long countQuadraples(long N)
{
     
    // Counter variable
    long cnt = 0;
 
    // Map to store the
    // sum of pair (a^2 + b^2)
    Map m = new HashMap<>();
 
    // Iterate till N
    for(long a = 1; a <= N; a++)
    {
        for(long b = 1; b <= N; b++)
        {
             
            // Calculate a^2 + b^2
            long x = a * a + b * b;
 
            // Increment the value in map
            m.put(x, m.getOrDefault(x, 0l) + 1);
        }
    }
     
    for(long c = 1; c <= N; c++)
    {
        for(long d = 1; d <= N; d++)
        {
            long x = c * c + d * d;
 
            // Check if this sum
            // was also in a^2 + b^2
            if (m.containsKey(x))
                cnt += m.get(x);
        }
    }
     
    // Return the count
    return cnt;
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given N
    long N = 2;
     
    // Function call
    System.out.println(countQuadraples(N));
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program for
# the above approach
from collections import defaultdict
 
# Function to count
# the quadruples
def countQuadraples(N):
 
    # Counter variable
    cnt = 0
  
    # Map to store the
    # sum of pair (a^2 + b^2)
    m = defaultdict (int)
  
    # Iterate till N
    for a in range (1, N + 1):
  
        for b in range (1, N + 1):
  
            # Calculate a^2 + b^2
            x = a * a + b * b
  
            # Increment the value in map
            m[x] += 1
  
    for c in range (1, N + 1):
        for d in range (1, N + 1):
  
            x = c * c + d * d
  
            # Check if this sum
            # was also in a^2 + b^2
            if x in m:
                cnt += m[x]
         
    # Return the count
    return cnt
  
# Driver Code
if __name__ == "__main__":
 
    # Given N
    N = 2
  
    # Function Call
    print (countQuadraples(N))
         
# This code is contributed by Chitranayal


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to count the quadruples
static long countQuadraples(long N)
{
     
    // Counter variable
    long cnt = 0;
 
    // Map to store the
    // sum of pair (a^2 + b^2)
    Dictionary m = new Dictionary();
 
    // Iterate till N
    for(long a = 1; a <= N; a++)
    {
        for(long b = 1; b <= N; b++)
        {
             
            // Calculate a^2 + b^2
            long x = a * a + b * b;
 
            // Increment the value in map
            if (m.ContainsKey(x))
                m[x] = m[x] + 1;
            else
                m.Add(x, 1);
        }
    }
     
    for(long c = 1; c <= N; c++)
    {
        for(long d = 1; d <= N; d++)
        {
            long x = c * c + d * d;
 
            // Check if this sum
            // was also in a^2 + b^2
            if (m.ContainsKey(x))
                cnt += m[x];
        }
    }
     
    // Return the count
    return cnt;
}
 
// Driver code
public static void Main(String[] args)
{
     
    // Given N
    long N = 2;
     
    // Function call
    Console.WriteLine(countQuadraples(N));
}
}
 
// This code is contributed by Amit Katiyar


输出:
6




时间复杂度: O(N 2 log N)
辅助空间: O(N)