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

📅  最后修改于: 2021-09-06 06:23:30             🧑  作者: 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


Javascript


输出:
6

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