# Count of a, b & c after n seconds for given reproduction rate

Given three different alphabets **‘a’**, **‘b’** and **‘c’** with a certain rule that after every **2 seconds** every **‘a’** changes to a **‘b’**, after every **5 seconds** every **‘b’** changes to one **‘c’** and after every **12 seconds** every **‘c’** changes again into **two ‘a’s**.

Starting with one **‘a’**, the task is to find the final count of **a**, **b** and **c** after given **n seconds**.

**Examples:**

Input:n = 2

Output:a = 0, b = 1, c = 0

Initially a = 1, b = 0, c = 0

At n = 1, nothing will change

At n = 2, all a will change to b i.e. a = 0, b = 1, c = 0

Input:n = 72

Output:a = 64, b = 0, c = 0

**Approach:** It can be observed that the values of a, b and c will form a pattern after every 60 seconds (which is the LCM of 2, 5 and 12) as follows:

- At n = 60 -> a = 32
^{1}, b = 0, c = 0 - At n = 120 -> a = 32
^{2}, b = 0, c = 0 - At n = 180 -> a = 32
^{3}, b = 0, c = 0 and so on.

If **n** is a multiple of **60** then calculate the result from the above observation else calculate the result for the multiple of **60** which is nearest to **n** say **x** and then update the result for the seconds from **x + 1** to **n**.

Below is the implementation of the above approach:

## 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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to prthe 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` `; ` ` ` ` ` `# Prthe 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## PHP

**Output:**

a = 64, b = 0, c = 0

## Recommended Posts:

- Converting seconds into days, hours, minutes and seconds
- Program to find the rate percentage from compound interest of consecutive years
- Program for converting hours into minutes and seconds
- Count numbers < = N whose difference with the count of primes upto them is > = K
- Count pairs with given sum | Set 2
- Count of all possible values of X such that A % X = B
- Count Divisors of n in O(n^1/3)
- Count pairs with Odd XOR
- Count rotations of N which are Odd and Even
- Count numbers that don't contain 3
- Count Pairs from two arrays with even sum
- Count Hexadecimal Number
- Count rotations divisible by 8
- Count of numbers satisfying m + sum(m) + sum(sum(m)) = N
- Count of pairs (x, y) in an array such that x < y

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.