X-Sums Sudoku
Time limit4sMemory limit1024 MB
Given n, m, a direction, and an index, find the X-sum of that row or column in the lexicographically smallest 2^n by 2^m sudoku.
- Level
Hard8 of 10
- Topics
- Math, Bit manipulation, Recursion
- Solved
- No attempts yet
Problem
An sudoku puzzle is a grid made of regions, and each region contains cells. Hence an sudoku puzzle contains cells. Every integer from to occurs exactly once in each row, each column, and each region.
List the integers in a row or a column from some direction as a sequence of length . Let be the first integer of the sequence. The X-sum is the sum of the first integers of the sequence.

The figure above shows a sudoku puzzle with X-sums. The 7th row, listed from right to left, is . The first integer is , so the X-sum of the 7th row from the right is .
Given two positive integers and , a direction , and an index , find the X-sum of the -th row or -th column from the direction in the lexicographically smallest sudoku.
For two sudoku puzzles and of the same size, is lexicographically smaller than if there exist and such that , for all , and for and .
Input
The first line contains an integer (), the number of test cases.
Each test case is one line with two integers and (), a string , and an integer (). The puzzle size is . The direction is one of left, right, top, or bottom. The index identifies a row or a column.
Output
For each test case, print one integer on its own line: the X-sum of the -th row or -th column from the direction in the lexicographically smallest sudoku.
The answer can exceed .