Walsh Matrix
Time limit1sMemory limit128 MB
Sum entries in one row of a Walsh matrix over columns S through E, where the matrix size 2^N can reach 2^60.
- Level
Medium7 of 10
- Topics
- Divide and conquer, Recursion, Math, Bit manipulation
- Solved
- No attempts yet
Problem
A Walsh matrix is a square matrix whose size is a power of two and whose every entry is either or .
Its defining property is that the scalar (dot) product of any two distinct rows (or of any two distinct columns) — the sum of the products of entries at matching positions — is always .
The Walsh matrix of size has a single entry equal to . The Walsh matrix of size is built from four copies of the Walsh matrix of size :
That is, the top-left, top-right, and bottom-left blocks each hold unchanged, while the bottom-right block holds , the same matrix with every entry's sign flipped.
Rows are numbered from top to bottom, and columns from left to right. Given integers , write a program that computes the sum of the entries in row , from column through column , of the Walsh matrix of size .
Input
The input consists of several test cases. Each test case is a single line with four integers . (, , , )
The last line contains four values and must not be processed.
Output
For each test case, print the computed sum on its own line.