Dado un entero K y una array arr[] , la tarea es contar todas las sub-arrays cuyo producto es divisible por K .
Ejemplos:
Entrada: arr[] = {6, 2, 8}, K = 4
Salida: 4
Las subarreglas requeridas son {6, 2}, {6, 2, 8}, {2, 8} y {8}.
Entrada: arr[] = {9, 1, 14}, K = 6
Salida: 1
Enfoque ingenuo: ejecute bucles anidados y verifique para cada subarreglo si el producto % k == 0 . Actualice el conteo = conteo + 1 cuando la condición sea verdadera para el subarreglo. La complejidad temporal de este enfoque es O(n 3 ) .
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; #define ll long long // Function to count sub-arrays whose // product is divisible by K int countSubarrays(const int* arr, int n, int K) { int count = 0; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { // Calculate the product of the // current sub-array ll product = 1; for (int x = i; x <= j; x++) product *= arr[x]; // If product of the current sub-array // is divisible by K if (product % K == 0) count++; } } return count; } // Driver code int main() { int arr[] = { 6, 2, 8 }; int n = sizeof(arr) / sizeof(arr[0]); int K = 4; cout << countSubarrays(arr, n, K); return 0; }
Java
// Java implementation of the approach import java.util.*; class GFG { // Function to count sub-arrays whose // product is divisible by K static int countSubarrays(int []arr, int n, int K) { int count = 0; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { // Calculate the product of the // current sub-array long product = 1; for (int x = i; x <= j; x++) product *= arr[x]; // If product of the current sub-array // is divisible by K if (product % K == 0) count++; } } return count; } // Driver code public static void main(String[] args) { int arr[] = { 6, 2, 8 }; int n = arr.length; int K = 4; System.out.println(countSubarrays(arr, n, K)); } } // This code contributed by Rajput-Ji
Python 3
# Python 3 implementation of the approach # Function to count sub-arrays whose # product is divisible by K def countSubarrays(arr, n, K): count = 0 for i in range( n): for j in range(i, n): # Calculate the product of # the current sub-array product = 1 for x in range(i, j + 1): product *= arr[x] # If product of the current # sub-array is divisible by K if (product % K == 0): count += 1 return count # Driver code if __name__ == "__main__": arr = [ 6, 2, 8 ] n = len(arr) K = 4 print(countSubarrays(arr, n, K)) # This code is contributed by ita_c
C#
// C# implementation of the approach using System; class GFG { // Function to count sub-arrays whose // product is divisible by K static int countSubarrays(int []arr, int n, int K) { int count = 0; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { // Calculate the product of the // current sub-array long product = 1; for (int x = i; x <= j; x++) product *= arr[x]; // If product of the current sub-array // is divisible by K if (product % K == 0) count++; } } return count; } // Driver code public static void Main() { int []arr = { 6, 2, 8 }; int n = arr.Length; int K = 4; Console.WriteLine(countSubarrays(arr, n, K)); } } // This code contributed by Rajput-Ji
PHP
<?php // PHP implementation of the approach // Function to count sub-arrays whose // product is divisible by K function countSubarrays($arr, $n, $K) { $count = 0; for ($i = 0; $i < $n; $i++) { for ($j = $i; $j < $n; $j++) { // Calculate the product of the // current sub-array $product = 1; for ($x = $i; $x <= $j; $x++) $product *= $arr[$x]; // If product of the current // sub-array is divisible by K if ($product % $K == 0) $count++; } } return $count; } // Driver code $arr = array( 6, 2, 8 ); $n = count($arr); $K = 4; echo countSubarrays($arr, $n, $K); // This code is contributed by mits ?>
Javascript
<script> // Javascript implementation of the approach // Function to count sub-arrays whose // product is divisible by K function countSubarrays(arr, n, K) { let count = 0; for (let i = 0; i < n; i++) { for (let j = i; j < n; j++) { // Calculate the product of the // current sub-array let product = 1; for (let x = i; x <= j; x++) product *= arr[x]; // If product of the current sub-array // is divisible by K if (product % K == 0) count++; } } return count; } // Driver code let arr = [ 6, 2, 8 ]; let n = arr.length; let K = 4; document.write(countSubarrays(arr, n, K)); // This ode is contributed by mukesh07. </script>
4
Un mejor enfoque es usar la técnica de dos punteros o usar árboles de segmentos para encontrar el producto de un subarreglo en poco tiempo.
Árboles de segmentos: La complejidad de la solución ingenua es cúbica. Esto se debe a que se recorre cada subarreglo para encontrar el producto. En lugar de atravesar cada subarreglo, los productos se pueden almacenar en un árbol de segmentos y se puede consultar el árbol para obtener el valor del producto % k en tiempo O (log n) . Por lo tanto, la complejidad temporal de este enfoque sería O(n 2 log n) .
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; #define ll long long #define MAX 100002 // Segment tree implemented as an array ll tree[4 * MAX]; // Function to build the segment tree void build(int node, int start, int end, const int* arr, int k) { if (start == end) { tree[node] = (1LL * arr[start]) % k; return; } int mid = (start + end) >> 1; build(2 * node, start, mid, arr, k); build(2 * node + 1, mid + 1, end, arr, k); tree[node] = (tree[2 * node] * tree[2 * node + 1]) % k; } // Function to query product of // sub-array[l..r] in O(log n) time ll query(int node, int start, int end, int l, int r, int k) { if (start > end || start > r || end < l) { return 1; } if (start >= l && end <= r) { return tree[node] % k; } int mid = (start + end) >> 1; ll q1 = query(2 * node, start, mid, l, r, k); ll q2 = query(2 * node + 1, mid + 1, end, l, r, k); return (q1 * q2) % k; } // Function to count sub-arrays whose // product is divisible by K ll countSubarrays(const int* arr, int n, int k) { ll count = 0; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { // Query segment tree to find product % k // of the sub-array[i..j] ll product_mod_k = query(1, 0, n - 1, i, j, k); if (product_mod_k == 0) { count++; } } } return count; } // Driver code int main() { int arr[] = { 6, 2, 8 }; int n = sizeof(arr) / sizeof(arr[0]); int k = 4; // Build the segment tree build(1, 0, n - 1, arr, k); cout << countSubarrays(arr, n, k); return 0; }
Java
// Java implementation for above approach class GFG { static int MAX = 100002; // Segment tree implemented as an array static long tree[] = new long[4 * MAX]; // Function to build the segment tree static void build(int node, int start, int end, int []arr, int k) { if (start == end) { tree[node] = (1L * arr[start]) % k; return; } int mid = (start + end) >> 1; build(2 * node, start, mid, arr, k); build(2 * node + 1, mid + 1, end, arr, k); tree[node] = (tree[2 * node] * tree[2 * node + 1]) % k; } // Function to query product of // sub-array[l..r] in O(log n) time static long query(int node, int start, int end, int l, int r, int k) { if (start > end || start > r || end < l) { return 1; } if (start >= l && end <= r) { return tree[node] % k; } int mid = (start + end) >> 1; long q1 = query(2 * node, start, mid, l, r, k); long q2 = query(2 * node + 1, mid + 1, end, l, r, k); return (q1 * q2) % k; } // Function to count sub-arrays whose // product is divisible by K static long countSubarrays(int []arr, int n, int k) { long count = 0; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { // Query segment tree to find product % k // of the sub-array[i..j] long product_mod_k = query(1, 0, n - 1, i, j, k); if (product_mod_k == 0) { count++; } } } return count; } // Driver code public static void main(String[] args) { int arr[] = { 6, 2, 8 }; int n = arr.length; int k = 4; // Build the segment tree build(1, 0, n - 1, arr, k); System.out.println(countSubarrays(arr, n, k)); } } // This code has been contributed by 29AjayKumar
Python3
# Python3 implementation of the approach MAX = 100002 # Segment tree implemented as an array tree = [0 for i in range(4 * MAX)]; # Function to build the segment tree def build(node, start, end, arr, k): if (start == end): tree[node] = (arr[start]) % k; return; mid = (start + end) >> 1; build(2 * node, start, mid, arr, k); build(2 * node + 1, mid + 1, end, arr, k); tree[node] = (tree[2 * node] * tree[2 * node + 1]) % k; # Function to query product of # sub-array[l..r] in O(log n) time def query(node, start, end, l, r, k): if (start > end or start > r or end < l): return 1; if (start >= l and end <= r): return tree[node] % k; mid = (start + end) >> 1; q1 = query(2 * node, start, mid, l, r, k); q2 = query(2 * node + 1, mid + 1, end, l, r, k); return (q1 * q2) % k; # Function to count sub-arrays whose # product is divisible by K def countSubarrays(arr, n, k): count = 0; for i in range(n): for j in range(i, n): # Query segment tree to find product % k # of the sub-array[i..j] product_mod_k = query(1, 0, n - 1, i, j, k); if (product_mod_k == 0): count += 1 return count; # Driver code if __name__=='__main__': arr = [ 6, 2, 8 ] n = len(arr) k = 4; # Build the segment tree build(1, 0, n - 1, arr, k); print(countSubarrays(arr, n, k)) # This code is contributed by pratham76.
C#
// C# implementation for above approach using System; class GFG { static int MAX = 100002; // Segment tree implemented as an array static long []tree = new long[4 * MAX]; // Function to build the segment tree static void build(int node, int start, int end, int []arr, int k) { if (start == end) { tree[node] = (1L * arr[start]) % k; return; } int mid = (start + end) >> 1; build(2 * node, start, mid, arr, k); build(2 * node + 1, mid + 1, end, arr, k); tree[node] = (tree[2 * node] * tree[2 * node + 1]) % k; } // Function to query product of // sub-array[l..r] in O(log n) time static long query(int node, int start, int end, int l, int r, int k) { if (start > end || start > r || end < l) { return 1; } if (start >= l && end <= r) { return tree[node] % k; } int mid = (start + end) >> 1; long q1 = query(2 * node, start, mid, l, r, k); long q2 = query(2 * node + 1, mid + 1, end, l, r, k); return (q1 * q2) % k; } // Function to count sub-arrays whose // product is divisible by K static long countSubarrays(int []arr, int n, int k) { long count = 0; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { // Query segment tree to find product % k // of the sub-array[i..j] long product_mod_k = query(1, 0, n - 1, i, j, k); if (product_mod_k == 0) { count++; } } } return count; } // Driver code public static void Main(String[] args) { int []arr = { 6, 2, 8 }; int n = arr.Length; int k = 4; // Build the segment tree build(1, 0, n - 1, arr, k); Console.WriteLine(countSubarrays(arr, n, k)); } } /* This code contributed by PrinciRaj1992 */
Javascript
<script> // JavaScript implementation for above approach let MAX = 100002; // Segment tree implemented as an array let tree = new Array(4 * MAX); // Function to build the segment tree function build(node,start,end,arr,k) { if (start == end) { tree[node] = (1 * arr[start]) % k; return; } let mid = (start + end) >> 1; build(2 * node, start, mid, arr, k); build(2 * node + 1, mid + 1, end, arr, k); tree[node] = (tree[2 * node] * tree[2 * node + 1]) % k; } // Function to query product of // sub-array[l..r] in O(log n) time function query(node,start,end,l,r,k) { if (start > end || start > r || end < l) { return 1; } if (start >= l && end <= r) { return tree[node] % k; } let mid = (start + end) >> 1; let q1 = query(2 * node, start, mid, l, r, k); let q2 = query(2 * node + 1, mid + 1, end, l, r, k); return (q1 * q2) % k; } // Function to count sub-arrays whose // product is divisible by K function countSubarrays(arr,n,k) { let count = 0; for (let i = 0; i < n; i++) { for (let j = i; j < n; j++) { // Query segment tree to find product % k // of the sub-array[i..j] let product_mod_k = query(1, 0, n - 1, i, j, k); if (product_mod_k == 0) { count++; } } } return count; } // Driver code let arr=[ 6, 2, 8]; let n = arr.length; let k = 4; // Build the segment tree build(1, 0, n - 1, arr, k); document.write(countSubarrays(arr, n, k)); // This code is contributed by rag2127 </script>
4
Optimizando aún más la solución: es comprensible que si el producto de un subarreglo [i..j] es divisible por k, entonces el producto de todos los subarreglos [i..t] tales que j < t < n también será divisible por k . Por lo tanto , la búsqueda binaria se puede aplicar para contar subarreglos que comienzan en un índice particular i y cuyo producto es divisible por k.
CPP
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; #define MAX 100005 typedef long long ll; // Segment tree implemented as an array ll tree[MAX << 2]; // Function to build segment tree void build(int node, int start, int end, const int* arr, int k) { if (start == end) { tree[node] = arr[start] % k; return; } int mid = (start + end) >> 1; build(2 * node, start, mid, arr, k); build(2 * node + 1, mid + 1, end, arr, k); tree[node] = (tree[2 * node] * tree[2 * node + 1]) % k; } // Function to query product % k // of sub-array[l..r] ll query(int node, int start, int end, int l, int r, int k) { if (start > end || start > r || end < l) return 1; if (start >= l && end <= r) return tree[node] % k; int mid = (start + end) >> 1; ll q1 = query(2 * node, start, mid, l, r, k); ll q2 = query(2 * node + 1, mid + 1, end, l, r, k); return (q1 * q2) % k; } // Function to return the count of sub-arrays // whose product is divisible by K ll countSubarrays(int* arr, int n, int k) { ll ans = 0; for (int i = 0; i < n; i++) { int low = i, high = n - 1; // Binary search // Check if sub-array[i..mid] satisfies the constraint // Adjust low and high accordingly while (low <= high) { int mid = (low + high) >> 1; if (query(1, 0, n - 1, i, mid, k) == 0) high = mid - 1; else low = mid + 1; } ans += n - low; } return ans; } // Driver code int main() { int arr[] = { 6, 2, 8 }; int n = sizeof(arr) / sizeof(arr[0]); int k = 4; // Build the segment tree build(1, 0, n - 1, arr, k); cout << countSubarrays(arr, n, k); return 0; }
Java
// Java implementation of the approach class GFG { static int MAX = 100005; // Segment tree implemented as an array static int tree[] = new int[MAX << 2]; // Function to build segment tree static void build(int node, int start, int end, int arr[], int k) { if (start == end) { tree[node] = arr[start] % k; return; } int mid = (start + end) >> 1; build(2 * node, start, mid, arr, k); build(2 * node + 1, mid + 1, end, arr, k); tree[node] = (tree[2 * node] * tree[2 * node + 1]) % k; } // Function to query product % k // of sub-array[l..r] static int query(int node, int start, int end, int l, int r, int k) { if (start > end || start > r || end < l) return 1; if (start >= l && end <= r) return tree[node] % k; int mid = (start + end) >> 1; int q1 = query(2 * node, start, mid, l, r, k); int q2 = query(2 * node + 1, mid + 1, end, l, r, k); return (q1 * q2) % k; } // Function to return the count of sub-arrays // whose product is divisible by K static int countSubarrays(int arr[], int n, int k) { int ans = 0; for (int i = 0; i < n; i++) { int low = i, high = n - 1; // Binary search // Check if sub-array[i..mid] satisfies the constraint // Adjust low and high accordingly while (low <= high) { int mid = (low + high) >> 1; if (query(1, 0, n - 1, i, mid, k) == 0) high = mid - 1; else low = mid + 1; } ans += n - low; } return ans; } // Driver code public static void main(String[] args) { int arr[] = { 6, 2, 8 }; int n = arr.length; int k = 4; // Build the segment tree build(1, 0, n - 1, arr, k); System.out.println(countSubarrays(arr, n, k)); } } // This code is contributed by divyesh072019
Python3
# Python3 implementation of the approach MAX = 100005 # Segment tree implemented as an array tree = [0 for i in range(MAX << 2)]; # Function to build segment tree def build(node, start, end, arr, k): if (start == end): tree[node] = arr[start] % k; return; mid = (start + end) >> 1; build(2 * node, start, mid, arr, k); build(2 * node + 1, mid + 1, end, arr, k); tree[node] = (tree[2 * node] * tree[2 * node + 1]) % k; # Function to query product % k # of sub-array[l..r] def query(node, start, end, l, r, k): if (start > end or start > r or end < l): return 1; if (start >= l and end <= r): return tree[node] % k; mid = (start + end) >> 1; q1 = query(2 * node, start, mid, l, r, k); q2 = query(2 * node + 1, mid + 1, end, l, r, k); return (q1 * q2) % k; # Function to return the count of sub-arrays # whose product is divisible by K def countSubarrays(arr, n, k): ans = 0; for i in range(n): low = i high = n - 1; # Binary search # Check if sub-array[i..mid] satisfies the constraint # Adjust low and high accordingly while (low <= high): mid = (low + high) >> 1; if (query(1, 0, n - 1, i, mid, k) == 0): high = mid - 1; else: low = mid + 1; ans += n - low; return ans; # Driver code if __name__=='__main__': arr = [ 6, 2, 8 ] n = len(arr) k = 4; # Build the segment tree build(1, 0, n - 1, arr, k); print(countSubarrays(arr, n, k)) # This code is contributed by rutvik_56.
C#
// C# implementation of the approach using System; using System.Collections.Generic; class GFG { static int MAX = 100005; // Segment tree implemented as an array static int[] tree = new int[MAX << 2]; // Function to build segment tree static void build(int node, int start, int end, int[] arr, int k) { if (start == end) { tree[node] = arr[start] % k; return; } int mid = (start + end) >> 1; build(2 * node, start, mid, arr, k); build(2 * node + 1, mid + 1, end, arr, k); tree[node] = (tree[2 * node] * tree[2 * node + 1]) % k; } // Function to query product % k // of sub-array[l..r] static int query(int node, int start, int end, int l, int r, int k) { if (start > end || start > r || end < l) return 1; if (start >= l && end <= r) return tree[node] % k; int mid = (start + end) >> 1; int q1 = query(2 * node, start, mid, l, r, k); int q2 = query(2 * node + 1, mid + 1, end, l, r, k); return (q1 * q2) % k; } // Function to return the count of sub-arrays // whose product is divisible by K static int countSubarrays(int[] arr, int n, int k) { int ans = 0; for (int i = 0; i < n; i++) { int low = i, high = n - 1; // Binary search // Check if sub-array[i..mid] satisfies the constraint // Adjust low and high accordingly while (low <= high) { int mid = (low + high) >> 1; if (query(1, 0, n - 1, i, mid, k) == 0) high = mid - 1; else low = mid + 1; } ans += n - low; } return ans; } // Driver code static void Main() { int[] arr = { 6, 2, 8 }; int n = arr.Length; int k = 4; // Build the segment tree build(1, 0, n - 1, arr, k); Console.Write(countSubarrays(arr, n, k)); } } // This code is contributed by divyeshrbadiya07
Javascript
<script> // Javascript implementation of the approach let MAX = 100005; // Segment tree implemented as an array let tree = new Array(MAX << 2); // Function to build segment tree function build(node, start, end, arr, k) { if (start == end) { tree[node] = arr[start] % k; return; } let mid = (start + end) >> 1; build(2 * node, start, mid, arr, k); build(2 * node + 1, mid + 1, end, arr, k); tree[node] = (tree[2 * node] * tree[2 * node + 1]) % k; } // Function to query product % k // of sub-array[l..r] function query(node,start,end,l,r,k) { if (start > end || start > r || end < l) return 1; if (start >= l && end <= r) return tree[node] % k; let mid = (start + end) >> 1; let q1 = query(2 * node, start, mid, l, r, k); let q2 = query(2 * node + 1, mid + 1, end, l, r, k); return (q1 * q2) % k; } // Function to return the count of sub-arrays // whose product is divisible by K function countSubarrays(arr,n,k) { let ans = 0; for (let i = 0; i < n; i++) { let low = i, high = n - 1; // Binary search // Check if sub-array[i..mid] satisfies the constraint // Adjust low and high accordingly while (low <= high) { let mid = (low + high) >> 1; if (query(1, 0, n - 1, i, mid, k) == 0) high = mid - 1; else low = mid + 1; } ans += n - low; } return ans; } // Driver code let arr = [6, 2, 8]; let n = arr.length; let k = 4; // Build the segment tree build(1, 0, n - 1, arr, k); document.write(countSubarrays(arr, n, k)); // This code is contributed by avanitrachhadiya2155. </script>
4
Técnica de dos punteros: análoga a la discusión de búsqueda binaria, está claro que si el subconjunto [i..j] tiene un producto divisible por k , entonces todos los subconjuntos [i..t] tales que j < t < n también tendrá productos divisibles por k .
Por lo tanto, la técnica de dos punteros también se puede aplicar aquí en correspondencia con el hecho anterior. Se toman dos punteros l, r con l apuntando al inicio del subarreglo actual y r apuntando al final del subarreglo actual . Si el subconjunto [l..r] tiene un producto divisible por k, entonces todos los subconjuntos [l..s] tales que r < s < n tendrán un producto divisible por k. Por lo tanto, realice cuenta = cuenta + n – r.Dado que es necesario agregar y eliminar elementos del subconjunto actual, simplemente no se pueden tomar productos de todo el subconjunto, ya que será engorroso agregar y eliminar elementos de esta manera.
En cambio, k se factoriza en tiempo O(sqrt(n)) y sus factores primos se almacenan en un mapa STL. Se utiliza otro mapa para mantener los recuentos de números primos en el subarreglo actual que puede llamarse mapa actual en este contexto. Luego, siempre que sea necesario agregar un elemento en la subarray actual, el recuento de esos números primos se agrega al mapa actual que ocurre en la descomposición en factores primos de k . Siempre que sea necesario eliminar un elemento del mapa actual, entonces el conteo de números primos se resta de manera similar.
A continuación se muestra la implementación del enfoque anterior.
CPP
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; #define ll long long #define MAX 100002 #define pb push_back // Vector to store primes vector<int> primes; // k_cnt stores count of prime factors of k // current_map stores the count of primes // in the current sub-array // cnts[] is an array of maps which stores // the count of primes for element at index i unordered_map<int, int> k_cnt, current_map, cnts[MAX]; // Function to store primes in // the vector primes void sieve() { int prime[MAX]; prime[0] = prime[1] = 1; for (int i = 2; i < MAX; i++) { if (prime[i] == 0) { for (int j = i * 2; j < MAX; j += i) { if (prime[j] == 0) { prime[j] = i; } } } } for (int i = 2; i < MAX; i++) { if (prime[i] == 0) { prime[i] = i; primes.pb(i); } } } // Function to count sub-arrays whose product // is divisible by k ll countSubarrays(int* arr, int n, int k) { // Special case if (k == 1) { cout << (1LL * n * (n + 1)) / 2; return 0; } vector<int> k_primes; for (auto p : primes) { while (k % p == 0) { k_primes.pb(p); k /= p; } } // If k is prime and is more than 10^6 if (k > 1) { k_primes.pb(k); } for (auto num : k_primes) { k_cnt[num]++; } // Two pointers initialized int l = 0, r = 0; ll ans = 0; while (r < n) { // Add rth element to the current segment for (auto& it : k_cnt) { // p = prime factor of k int p = it.first; while (arr[r] % p == 0) { current_map[p]++; cnts[r][p]++; arr[r] /= p; } } // Check if current sub-array's product // is divisible by k int flag = 0; for (auto& it : k_cnt) { int p = it.first; if (current_map[p] < k_cnt[p]) { flag = 1; break; } } // If for all prime factors p of k, // current_map[p] >= k_cnt[p] // then current sub-array is divisible by k if (!flag) { // flag = 0 means that after adding rth element // segment's product is divisible by k ans += n - r; // Eliminate 'l' from the current segment for (auto& it : k_cnt) { int p = it.first; current_map[p] -= cnts[l][p]; } l++; } else { r++; } } return ans; } // Driver code int main() { int arr[] = { 6, 2, 8 }; int n = sizeof(arr) / sizeof(arr[0]); int k = 4; sieve(); cout << countSubarrays(arr, n, k); return 0; }
4
Publicación traducida automáticamente
Artículo escrito por rohan23chhabra y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA