#P4067. [SDOI2016] 储能表

[SDOI2016] 储能表

Description

There is a table with nn rows and mm columns, with rows numbered from 00 to n1n-1 and columns numbered from 00 to m1m-1. Each cell stores energy. Initially, the cell in row ii and column jj stores (ij)(i \oplus j) units of energy (where \oplus denotes bitwise XOR). Therefore, the total energy stored in the whole table is

i=0n1j=0m1ij.\sum_{i=0}^{n-1} \sum_{j=0}^{m-1} i \oplus j.

As time passes, the energy in the cells gradually decreases. After each unit of time, the energy in every cell decreases by 11. Obviously, once a cell’s energy decreases to 00, it will not decrease any further.

That is, after kk units of time, the total energy stored in the whole table is

$$\sum_{i=0}^{n-1} \sum_{j=0}^{m-1} \max((i \oplus j)-k,0).$$

Given the table, find the total energy it stores after kk units of time.

Since the total energy may be large, output it modulo pp.

Input Format

The first line contains an integer TT, the number of test cases. Then TT lines follow, each containing four integers n,m,k,pn, m, k, p.

Output Format

Output TT lines, each containing one number: the result of the total energy modulo pp.

3
2 2 0 100
3 3 0 100
3 3 1 100
2
12
6

Hint

For 100%100\% of the testdata, it is guaranteed that 1T50001 \le T \le 5000, 1p1091 \le p \le 10^9, 1n,m10181 \le n,m \le 10^{18}, 0k10180 \le k \le 10^{18}.

测试点编号 T=T= nn\le mm\le kk\le pp\le
1,21,2 50005000 100100 10910^9
33 101810^{18} 101810^{18} 00
44 11
55 1010 1010
66 11 10510^5 10510^5
77 101810^{18} 101810^{18}
88 100100
9,109,10 50005000

Statement fixed by Starrykiller.\texttt{Statement fixed by Starrykiller.}

Translated by ChatGPT 5