Dados dos enteros n y g , la tarea es generar una secuencia creciente de n enteros tal que:
- El mcd de todos los elementos de la sucesión es g .
- Y, la suma de todos los elementos es el mínimo entre todas las secuencias posibles.
Ejemplos:
Entrada: n = 6, g = 5
Salida: 5 10 15 20 25 30Entrada: n = 5, g = 3
Salida: 3 6 9 12 15
Planteamiento: La suma de la sucesión será mínima cuando la sucesión estará formada por los elementos:
g, 2*g, 3*g, 4*g, ….., n*g .
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 print the required sequence void generateSequence(int n, int g) { for (int i = 1; i <= n; i++) cout << i * g << " "; } // Driver Code int main() { int n = 6, g = 5; generateSequence(n, g); return 0; }
Java
// Java implementation of the approach class GFG { // Function to print the required sequence static void generateSequence(int n, int g) { for (int i = 1; i <= n; i++) System.out.print(i * g + " ");; } // Driver Code public static void main(String []args) { int n = 6, g = 5; generateSequence(n, g); } } // This code is contributed by Rituraj Jain
Python3
# Python3 implementation of the approach # Function to print the required sequence def generateSequence(n, g): for i in range(1, n + 1): print(i * g, end = " ") # Driver Code if __name__ == "__main__": n, g = 6, 5 generateSequence(n, g) # This code is contributed by Rituraj Jain
C#
// C# implementation of the approach using System ; class GFG { // Function to print the required sequence static void generateSequence(int n, int g) { for (int i = 1; i <= n; i++) Console.Write(i * g + " "); } // Driver Code public static void Main() { int n = 6, g = 5; generateSequence(n, g); } } // This code is contributed by Ryuga
PHP
<?php // PHP implementation of the approach // Function to print the required sequence function generateSequence($n, $g) { for ($i = 1; $i <= $n; $i++) echo $i * $g . " "; } // Driver Code $n = 6; $g = 5; generateSequence($n, $g); // This code is contributed by ita_c ?>
Javascript
<script> // Javascript implementation of the approach // Function to print the required sequence function generateSequence(n, g) { for (var i = 1; i <= n; i++) { document.write(i*g+" "); } } // Driver Code var n = 6, g = 5; generateSequence(n, g); </script>
Producción:
5 10 15 20 25 30
Complejidad de tiempo: O(n)
Espacio Auxiliar: O(1)