Dados dos números N y K , la tarea es encontrar la suma de los primeros K números que no son divisibles por N.
Ejemplos:
Entrada: N = 5, K = 10
Salida: 63
Explicación: La suma de { 1, 2, 3, 4, 6, 7, 8, 9, 11, 12 } es 63.Entrada: N = 3, k = 13
Salida: 127
Explicación: La suma de { 1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19 } es 127.
Planteamiento: Para resolver este problema, debemos seguir los siguientes pasos:
- Calcular el último múltiplo de N por (K / (N – 1)) * N
- Calcular K%(N – 1) . Si el resto es 0, (K / (N – 1)) * N – 1 da el último valor que no es divisible por N. De lo contrario, debemos sumar el resto a (K / (N – 1)) * N .
- Calcule la suma hasta el último valor que no es divisible por N obtenido en el paso anterior.
- Calcule la suma de múltiplos de N y reste de la suma calculada en el paso anterior para obtener el resultado deseado.
El siguiente código es la implementación del enfoque anterior:
C++
// C++ Program to calculate // the sum of first K // numbers not divisible by N #include <bits/stdc++.h> using namespace std; // Function to find the sum int findSum(int n, int k) { // Find the last multiple of N int val = (k / (n - 1)) * n; int rem = k % (n - 1); // Find the K-th non-multiple of N if (rem == 0) { val = val - 1; } else { val = val + rem; } // Calculate the sum of // all elements from 1 to val int sum = (val * (val + 1)) / 2; // Calculate the sum of // all multiples of N // between 1 to val int x = k / (n - 1); int sum_of_multiples = (x * (x + 1) * n) / 2; sum -= sum_of_multiples; return sum; } // Driver code int main() { int n = 7, k = 13; cout << findSum(n, k) << endl; }
Java
// Java program to calculate // the sum of first K numbers // not divisible by N import java.util.*; class GFG { // Function to find the sum static int findSum(int n, int k) { // Find the last multiple of N int val = (k / (n - 1)) * n; int rem = k % (n - 1); // Find the K-th non-multiple of N if (rem == 0) { val = val - 1; } else { val = val + rem; } // Calculate the sum of // all elements from 1 to val int sum = (val * (val + 1)) / 2; // Calculate the sum of // all multiples of N // between 1 to val int x = k / (n - 1); int sum_of_multiples = (x * (x + 1) * n) / 2; sum -= sum_of_multiples; return sum; } // Driver code public static void main(String[] args) { int n = 7, k = 13; System.out.println(findSum(n, k)); } } // This code is contributed by offbeat
Python3
# Python3 Program to calculate # the sum of first K # numbers not divisible by N # Function to find the sum def findSum(n, k): # Find the last multiple of N val = (k // (n - 1)) * n; rem = k % (n - 1); # Find the K-th non-multiple of N if (rem == 0): val = val - 1; else: val = val + rem; # Calculate the sum of # all elements from 1 to val sum = (val * (val + 1)) // 2; # Calculate the sum of # all multiples of N # between 1 to val x = k // (n - 1); sum_of_multiples = (x * (x + 1) * n) // 2; sum -= sum_of_multiples; return sum; # Driver code n = 7; k = 13; print(findSum(n, k)) # This code is contributed by Code_Mech
C#
// C# program to calculate // the sum of first K numbers // not divisible by N using System; class GFG{ // Function to find the sum static int findSum(int n, int k) { // Find the last multiple of N int val = (k / (n - 1)) * n; int rem = k % (n - 1); // Find the K-th non-multiple of N if (rem == 0) { val = val - 1; } else { val = val + rem; } // Calculate the sum of // all elements from 1 to val int sum = (val * (val + 1)) / 2; // Calculate the sum of // all multiples of N // between 1 to val int x = k / (n - 1); int sum_of_multiples = (x * (x + 1) * n) / 2; sum -= sum_of_multiples; return sum; } // Driver code public static void Main(String[] args) { int n = 7, k = 13; Console.WriteLine(findSum(n, k)); } } // This code is contributed by 29AjayKumar
Javascript
<script> // Javascript Program to calculate // the sum of first K // numbers not divisible by N // Function to find the sum function findSum(n, k) { // Find the last multiple of N var val = parseInt(k / (n - 1)) * n; var rem = k % (n - 1); // Find the K-th non-multiple of N if (rem == 0) { val = val - 1; } else { val = val + rem; } // Calculate the sum of // all elements from 1 to val var sum = parseInt((val * (val + 1)) / 2); // Calculate the sum of // all multiples of N // between 1 to val var x = parseInt(k / (n - 1)); var sum_of_multiples = parseInt( (x * (x + 1) * n) / 2); sum -= sum_of_multiples; return sum; } // Driver code var n = 7, k = 13; document.write(findSum(n, k)) // This code is contributed by rrrtnx </script>
Producción:
99
Complejidad de Tiempo: O(1)
Complejidad de Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por AmanGupta65 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA