How would you solve the N-Queens problem efficiently under a 20-minute time limit?
Implement a function that counts the valid ways to place n queens on an n x n chessboard so that no two queens share a row, column, or diagonal. Use efficient backtracking, such as bitmasks, rather than constructing every board.
Input is an integer n; return the number of valid arrangements as an integer. The required signature is def total_n_queens(n):.
def total_n_queens(n):