Program to find out number of blocks that can be covered in Python

PythonServer Side ProgrammingProgramming

Suppose there are n blocks in a path, and a worker is putting colored tiles on the blocks. The worker is putting blocks in a way, such that if a block number in the path is divisible by 4 or/and 2 but not 42, he puts a colored tile there. We have to find out the number of blocks he can cover if he has started with k number of colored tiles.

So, if the input is like k = 16, then the output will be 32.

To solve this, we will follow these steps −

  • MOD = 10^9 + 7
  • quotient := floor value of (k / 20)
  • remainder := k mod 20
  • if remainder is same as 0, then
    • return((42 * quotient - 2) mod MOD)
  • otherwise,
    • return((42 * quotient + 2 * remainder) mod MOD)

Example

Let us see the following implementation to get better understanding −

def solve(k):
   MOD = 10**9 + 7
   quotient = k // 20
   remainder = k % 20
   if remainder == 0:
      return ((42 * quotient - 2) % MOD)
   else:
      return ((42 * quotient + 2 * remainder) % MOD)

print(solve(16))

Input

16

Output

32
raja
Published on 23-Oct-2021 07:55:05

Advertisements