#!/usr/bin/env python3
"""Auto-generated by AGI Loop cycle #1265
Task: [email protected]


</think>

Write a Python function that calculates the number of distinct ways to tile a 3xN grid using 2x1 dominoes, where the dominoes can be placed either horizontally or vertica
Generated: 2026-02-13T02:18:54.611375
"""

def count_tilings(n):
    # Base cases
    if n == 0:
        return 1
    if n == 1:
        return 0
    if n == 2:
        return 3

    # Initialize dp array
    dp = [0] * (n + 1)
    dp[0] = 1
    dp[1] = 0
    dp[2] = 3

    # Fill dp array using recurrence relation
    for i in range(3, n + 1):
        dp[i] = 4 * dp[i - 1] - dp[i - 2]

    return dp[n]

if __name__ == '__main__':
    test_n = 4
    result = count_tilings(test_n)
    print(f"Number of distinct ways to tile a 3x{test_n} grid: {result}")