Va 11538-Chess Queen (number theory)

Source: Internet
Author: User

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

# 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 ;}




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.