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

Suppose there are n blocks in a path, and a worker is putting colored tiles on the blocks. The worker puts tiles on blocks whose numbers are divisible by 4 or 2, but not by 42. We need to find how many blocks can be covered with k colored tiles.

The key insight is that in every 42 consecutive blocks, exactly 20 blocks satisfy our condition (divisible by 2 or 4, but not by 42). Since each tile covers 2 blocks, we can cover 42 blocks with 20 tiles in each complete cycle.

Algorithm

To solve this problem, we follow these steps ?

  • Calculate complete cycles: quotient = k // 20
  • Find remaining tiles: remainder = k % 20
  • Each complete cycle covers 42 blocks using 20 tiles
  • Remaining tiles cover 2 blocks each
  • Handle edge case when remainder is 0

Example

Let's implement the solution to understand better ?

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)

# Test with the given example
k = 16
result = solve(k)
print(f"With {k} tiles, we can cover {result} blocks")

The output of the above code is ?

With 16 tiles, we can cover 32 blocks

How It Works

For k = 16 tiles:

  • quotient = 16 // 20 = 0 (no complete cycles)
  • remainder = 16 % 20 = 16
  • Since remainder ? 0: result = 42 * 0 + 2 * 16 = 32 blocks

Testing with Multiple Cases

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)

# Test multiple cases
test_cases = [16, 20, 40, 60]

for k in test_cases:
    result = solve(k)
    print(f"k = {k}: {result} blocks covered")
k = 16: 32 blocks covered
k = 20: 40 blocks covered
k = 40: 82 blocks covered
k = 60: 124 blocks covered

Conclusion

This algorithm efficiently calculates the number of blocks that can be covered by recognizing the pattern of 42 blocks per 20 tiles. The modular arithmetic ensures the result fits within standard integer limits for large inputs.

Updated on: 2026-03-26T18:17:13+05:30

383 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements