Link to the question: Ultraviolet A 11538-Chess Queen
In an n? On m's board, put two queens, asking the two queens to attack each other and how many ways to put them.
Solution: because the Queen's attack range is vertical lines, horizontal lines, and diagonal lines, we can enumerate the positions of the two queens on each line. For example, there are eight diagonal lines, then there will be C (82) species for the two queens.
Number of rows n, m position C (m2 )? N
Number of columns m, n locations per column C (n2 )? M
Diagonal Line, 2? (2? Σ I = 1n? 1i? (I? 1) + (m? N + 1 )? N? (N? (1) because of the forward and forward slashes, multiply by 2.
The formula is simplified to 2? N? (N? 1 )? (3? M? N? 1) 3
Code:
# Include
# Include
# Include
Using namespace std; typedef unsigned long ll; ll n, m; int main () {while (cin> n> m) {if (! (N + m) break; if (m <n) swap (n, m); cout <n * m * (n + m-2) + 2 * n * (n-1) * (3 * m-n-1)/3 <endl;} return 0 ;}