Dados tres alfabetos diferentes ‘a’ , ‘b’ y ‘c’ con cierta regla que después de cada 2 segundos cada ‘a’ cambia a una ‘b’ , después de cada 5 segundos cada ‘b’ cambia a una ‘c’ y después de cada 12 segundos , cada ‘c’ cambia nuevamente a dos ‘a’s .
Comenzando con una ‘a’ , la tarea es encontrar la cuenta final de a , b y c después de n segundos dados .
Ejemplos:
Entrada: n = 2
Salida: a = 0, b = 1, c = 0
Inicialmente a = 1, b = 0, c = 0
En n = 1, nada cambiará
En n = 2, todo a cambiará a b es decir a = 0, b = 1, c = 0
Entrada: n = 72
Salida: a = 64, b = 0, c = 0
Enfoque: Se puede observar que los valores de a, b y c formarán un patrón cada 60 segundos (que es el MCM de 2, 5 y 12) de la siguiente manera:
- En n = 60 -> a = 32 1 , b = 0, c = 0
- En n = 120 -> a = 32 2 , b = 0, c = 0
- En n = 180 -> a = 32 3 , b = 0, c = 0 y así sucesivamente.
Si n es un múltiplo de 60 , calcule el resultado de la observación anterior; de lo contrario, calcule el resultado del múltiplo de 60 más cercano a n , digamos x , y luego actualice el resultado para los segundos de x + 1 a n .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; #define ull unsigned long long // Function to print the count of // a, b and c after n seconds void findCount(int n) { ull a = 1, b = 0, c = 0; // Number of multiples of 60 below n int x = n / 60; a = (ull)pow(32, x); // Multiple of 60 nearest to n x = 60 * x; for (int i = x + 1; i <= n; i++) { // Change all a to b if (i % 2 == 0) { b += a; a = 0; } // Change all b to c if (i % 5 == 0) { c += b; b = 0; } // Change each c to two a if (i % 12 == 0) { a += (2 * c); c = 0; } } // Print the updated values of a, b and c cout << "a = " << a << ", "; cout << "b = " << b << ", "; cout << "c = " << c; } // Driver code int main() { int n = 72; findCount(n); return 0; }
Java
// Java implementation of the approach class GFG { // Function to print the count of // a, b and c after n seconds static void findCount(int n) { long a = 1, b = 0, c = 0; // Number of multiples of 60 below n int x = n / 60; a = (long)Math.pow(32, x); // Multiple of 60 nearest to n x = 60 * x; for (int i = x + 1; i <= n; i++) { // Change all a to b if (i % 2 == 0) { b += a; a = 0; } // Change all b to c if (i % 5 == 0) { c += b; b = 0; } // Change each c to two a if (i % 12 == 0) { a += (2 * c); c = 0; } } // Print the updated values of a, b and c System.out.println("a = " + a + ", b = " + b + ", c = " + c); } // Driver code public static void main (String[] args) { int n = 72; findCount(n); } } // This code is contributed by mits
Python3
# Python3 implementation of the approach # Function to print the count of # a, b and c after n seconds import math def findCount(n): a, b, c = 1, 0, 0; # Number of multiples of 60 below n x = (int)(n / 60); a = int(math.pow(32, x)); # Multiple of 60 nearest to n x = 60 * x; for i in range(x + 1, n + 1): # Change all a to b if (i % 2 == 0): b += a; a = 0; # Change all b to c if (i % 5 == 0): c += b; b = 0; # Change each c to two a if (i % 12 == 0): a += (2 * c); c = 0; # Print the updated values of a, b and c print("a =", a, end = ", "); print("b =", b, end = ", "); print("c =", c); # Driver code if __name__ == '__main__': n = 72; findCount(n); # This code is contributed # by 29AjayKumar
C#
// C# implementation of the approach using System; class GFG { // Function to print the count of // a, b and c after n seconds static void findCount(int n) { long a = 1, b = 0, c = 0; // Number of multiples of 60 below n int x = n / 60; a = (long)Math.Pow(32, x); // Multiple of 60 nearest to n x = 60 * x; for (int i = x + 1; i <= n; i++) { // Change all a to b if (i % 2 == 0) { b += a; a = 0; } // Change all b to c if (i % 5 == 0) { c += b; b = 0; } // Change each c to two a if (i % 12 == 0) { a += (2 * c); c = 0; } } // Print the updated values of a, b and c Console.WriteLine("a = " + a + ", b = " + b + ", c = " + c); } // Driver code static void Main() { int n = 72; findCount(n); } } // This code is contributed by mits
PHP
<?php // PHP implementation of the approach // Function to print the count of // a, b and c after n seconds function findCount($n) { $a = 1; $b = 0; $c = 0; // Number of multiples of 60 below n $x = $n / 60; $a = pow(32, $x); // Multiple of 60 nearest to n $x = 60 * $x; for ($i = $x + 1; $i <= $n; $i++) { // Change all a to b if ($i % 2 == 0) { $b += $a; $a = 0; } // Change all b to c if ($i % 5 == 0) { $c += $b; $b = 0; } // Change each c to two a if ($i % 12 == 0) { $a += (2 * $c); $c = 0; } } // Print the updated values of a, b and c echo("a = " . $a . ", b = " . $b . ", c = " . $c); } // Driver code $n = 72; findCount($n); // This code is contributed // by Code_Mech. ?>
Javascript
<script> // JavaScript implementation of the approach // Function to print the count of // a, b and c after n seconds function findCount(n) { var a = 1, b = 0, c = 0; // Number of multiples of 60 below n var x = parseInt(n / 60); a = Math.pow(32, x); // Multiple of 60 nearest to n x = 60 * x; for (i = x + 1; i <= n; i++) { // Change all a to b if (i % 2 == 0) { b += a; a = 0; } // Change all b to c if (i % 5 == 0) { c += b; b = 0; } // Change each c to two a if (i % 12 == 0) { a += (2 * c); c = 0; } } // Print the updated values of a, b and c document.write("a = " + a + ", b = " + b + ", c = " + c); } // Driver code var n = 72; findCount(n); // This code contributed by Rajput-Ji </script>
a = 64, b = 0, c = 0
Complejidad de tiempo: O(n)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por Shivam.Pradhan y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA