Recuento de cuatrillizos del rango [L, R] con GCD igual a K

Dado un entero K y un rango [L, R] , la tarea es contar los pares de cuatrillizos del rango dado que tienen mcd igual a K .
Ejemplos: 
 

Entrada: L = 1, R = 5, K = 3 
Salida:
(3, 3, 3, 3) es el único cuatrillo válido con mcd = 3
Entrada: L = 2, R = 24, K = 5 
Salida: 239 
 

Enfoque ingenuo: podemos iterar sobre todos los números con cuatro bucles y para cada par de cuatrillizos verificar si su gcd es igual a K .
A continuación se muestra la implementación del enfoque anterior:
 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
 
 
// Function to return the count
// of quadruplets having gcd = k
int countQuadruplets(int l, int r, int k)
{
 
    // To store the required count
    int count = 0;
 
    // Check every quadruplet pair
    // whether its gcd is k
    for (int u = l; u <= r; u++) {
        for (int v = l; v <= r; v++) {
            for (int w = l; w <= r; w++) {
                for (int x = l; x <= r; x++) {
                    if (__gcd(__gcd(u, v), __gcd(w, x)) == k)
                        count++;
                }
            }
        }
    }
 
    // Return the required count
    return count;
}
 
// Driver code
int main()
{
    int l = 1, r = 10, k = 2;
 
    cout << countQuadruplets(l, r, k);
 
    return 0;
}

Java

// Java implementation of the approach
import java.io.*;
 
class GFG {
 
    // Function to return
    // the gcd of a and b
    static int gcd(int a, int b)
    {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }
 
    // Function to return the count
    // of quadruplets having gcd = k
    static int countQuadruplets(int l, int r, int k)
    {
 
        // To store the required count
        int count = 0;
 
        // Check every quadruplet pair
        // whether its gcd is k
        for (int u = l; u <= r; u++) {
            for (int v = l; v <= r; v++) {
                for (int w = l; w <= r; w++) {
                    for (int x = l; x <= r; x++) {
                        if (gcd(gcd(u, v), gcd(w, x)) == k)
                            count++;
                    }
                }
            }
        }
 
        // Return the required count
        return count;
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        int l = 1, r = 10, k = 2;
 
        System.out.println(countQuadruplets(l, r, k));
    }
}
 
// This code is contributed by jit_t.

Python 3

# Python 3 implementation of the approach
from math import gcd
 
# Function to return the count
# of quadruplets having gcd = k
def countQuadruplets(l, r, k):
     
    # To store the required count
    count = 0
 
    # Check every quadruplet pair
    # whether its gcd is k
    for u in range(l, r + 1 ,1):
        for v in range(l, r + 1, 1):
            for w in range(l, r + 1, 1):
                for x in range(l, r + 1, 1):
                    if (gcd(gcd(u, v), gcd(w, x)) == k):
                        count += 1
 
    # Return the required count
    return count
 
# Driver code
if __name__ == '__main__':
    l = 1
    r = 10
    k = 2
 
    print(countQuadruplets(l, r, k))
     
# This code is contributed
# by Surendra_Gangwar

C#

// C# implementation of the approach
using System;
 
class GFG {
 
    // Function to return
    // the gcd of a and b
    static int gcd(int a, int b)
    {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }
 
    // Function to return the count
    // of quadruplets having gcd = k
    static int countQuadruplets(int l, int r, int k)
    {
 
        // To store the required count
        int count = 0;
 
        // Check every quadruplet pair
        // whether its gcd is k
        for (int u = l; u <= r; u++) {
            for (int v = l; v <= r; v++) {
                for (int w = l; w <= r; w++) {
                    for (int x = l; x <= r; x++) {
                        if (gcd(gcd(u, v), gcd(w, x)) == k)
                            count++;
                    }
                }
            }
        }
 
        // Return the required count
        return count;
    }
 
    // Driver code
    static public void Main()
    {
        int l = 1, r = 10, k = 2;
        Console.WriteLine(countQuadruplets(l, r, k));
    }
}
 
// This code is contributed by ajit.

Javascript

<script>
 
// javascript implementation of the approach
 
    // Function to return
    // the gcd of a and b
    function gcd(a , b) {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }
 
    // Function to return the count
    // of quadruplets having gcd = k
    function countQuadruplets(l , r , k) {
 
        // To store the required count
        var count = 0;
 
        // Check every quadruplet pair
        // whether its gcd is k
        for (u = l; u <= r; u++) {
            for (v = l; v <= r; v++) {
                for (w = l; w <= r; w++) {
                    for (x = l; x <= r; x++) {
                        if (gcd(gcd(u, v), gcd(w, x)) == k)
                            count++;
                    }
                }
            }
        }
 
        // Return the required count
        return count;
    }
 
    // Driver code
     
 
        var l = 1, r = 10, k = 2;
 
        document.write(countQuadruplets(l, r, k));
 
// This code is contributed by todaysgaurav
 
</script>
Producción: 

607

 

Complejidad del Tiempo: O((r – l) 4 )

Espacio Auxiliar: O(1)
Aproximación Eficiente: 
 

  1. Encuentra el MCD de cada par posible (x, y) en el rango dado.
  2. Cuente las frecuencias de cada valor GCD posible.
  3. Después de eso, si el valor GCD de dos números es k, entonces incremente el conteo por frecuencia[i] * frecuencia[j].

A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include <iostream>
using namespace std;
 
// Function to return
// the gcd of a and b
int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
 
// Function to return the count
// of quadruplets having gcd = k
int countQuadruplets(int l, int r, int k)
{
 
    int frequency[r + 1] = { 0 };
 
    // Count the frequency of every possible gcd
    // value in the range
    for (int i = l; i <= r; i++) {
        for (int j = l; j <= r; j++) {
            frequency[gcd(i, j)]++;
        }
    }
 
    // To store the required count
    long long answer = 0;
 
    // Calculate the answer using frequency values
    for (int i = 1; i <= r; i++) {
        for (int j = 1; j <= r; j++) {
            if (gcd(i, j) == k) {
                answer += (frequency[i] * frequency[j]);
            }
        }
    }
 
    // Return the required count
    return answer;
}
 
// Driver code
int main()
{
    int l = 1, r = 10, k = 2;
 
    cout << countQuadruplets(l, r, k);
 
    return 0;
}

Java

// Java implementation of the approach
class GFG
{
     
// Function to return
// the gcd of a and b
static int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
 
// Function to return the count
// of quadruplets having gcd = k
static int countQuadruplets(int l, int r, int k)
{
 
    int frequency[]= new int[r + 1] ;
 
    // Count the frequency of every possible gcd
    // value in the range
    for (int i = l; i <= r; i++)
    {
        for (int j = l; j <= r; j++)
        {
            frequency[gcd(i, j)]++;
        }
    }
 
    // To store the required count
    long answer = 0;
 
    // Calculate the answer using frequency values
    for (int i = 1; i <= r; i++)
    {
        for (int j = 1; j <= r; j++)
        {
            if (gcd(i, j) == k)
            {
                answer += (frequency[i] * frequency[j]);
            }
        }
    }
 
    // Return the required count
    return (int)answer;
}
 
// Driver code
public static void main(String args[])
{
    int l = 1, r = 10, k = 2;
 
    System.out.println(countQuadruplets(l, r, k));
}
}
 
// This code is contributed by Arnab Kundu

Python3

# Python3 implementation of the approach
 
# Function to return
# the gcd of a and b
def gcd(a, b):
    if (b == 0):
        return a;
    return gcd(b, a % b);
 
# Function to return the count
# of quadruplets having gcd = k
def countQuadruplets(l, r, k):
    frequency = [0] * (r + 1);
 
    # Count the frequency of every possible gcd
    # value in the range
    for i in range(l, r + 1):
        for j in range(l, r + 1):
            frequency[gcd(i, j)] += 1;
 
    # To store the required count
    answer = 0;
 
    # Calculate the answer using frequency values
    for i in range(l, r + 1):
        for j in range(l, r + 1):
            if (gcd(i, j) == k):
                answer += (frequency[i] * frequency[j]);
 
    # Return the required count
    return answer;
 
# Driver code
if __name__ == '__main__':
    l, r, k = 1, 10, 2;
 
    print(countQuadruplets(l, r, k));
 
# This code is contributed by Rajput-Ji

C#

// C# implementation of the approach
using System;
 
class GFG
{
         
// Function to return
// the gcd of a and b
static int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
 
// Function to return the count
// of quadruplets having gcd = k
static int countQuadruplets(int l, int r, int k)
{
 
    int []frequency= new int[r + 1] ;
 
    // Count the frequency of every possible gcd
    // value in the range
    for (int i = l; i <= r; i++)
    {
        for (int j = l; j <= r; j++)
        {
            frequency[gcd(i, j)]++;
        }
    }
 
    // To store the required count
    long answer = 0;
 
    // Calculate the answer using frequency values
    for (int i = 1; i <= r; i++)
    {
        for (int j = 1; j <= r; j++)
        {
            if (gcd(i, j) == k)
            {
                answer += (frequency[i] * frequency[j]);
            }
        }
    }
 
    // Return the required count
    return (int)answer;
}
 
// Driver code
static public void Main ()
{
    int l = 1, r = 10, k = 2;
    Console.WriteLine(countQuadruplets(l, r, k));
}
}
 
// This code is contributed by @ajit_00023

Javascript

<script>
    // Javascript implementation of the approach
     
    // Function to return
    // the gcd of a and b
    function gcd(a, b)
    {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }
 
    // Function to return the count
    // of quadruplets having gcd = k
    function countQuadruplets(l, r, k)
    {
 
        let frequency= new Array(r + 1);
        frequency.fill(0);
 
        // Count the frequency of every possible gcd
        // value in the range
        for (let i = l; i <= r; i++)
        {
            for (let j = l; j <= r; j++)
            {
                frequency[gcd(i, j)]++;
            }
        }
 
        // To store the required count
        let answer = 0;
 
        // Calculate the answer using frequency values
        for (let i = 1; i <= r; i++)
        {
            for (let j = 1; j <= r; j++)
            {
                if (gcd(i, j) == k)
                {
                    answer += (frequency[i] * frequency[j]);
                }
            }
        }
 
        // Return the required count
        return answer;
    }
     
    let l = 1, r = 10, k = 2;
    document.write(countQuadruplets(l, r, k));
     
</script>
Producción: 

607

 

Tiempo Complejidad: O(r 2 )
 Espacio Auxiliar: O(r)

Publicación traducida automáticamente

Artículo escrito por p_unit y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *