分为皇后在同一行、同一列、同一对角线的情况分别计数相加即可。
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <climits> 5 using namespace std; 6 7 typedef long long ll; 8 9 ll a( ll n, ll m ) 10 { 11 return n * m * ( m - 1 ); 12 } 13 14 ll b( ll n, ll m ) 15 { 16 return a( m, n ); 17 } 18 19 ll d( ll n, ll m ) 20 { 21 return n * ( n - 1 ) * ( n - 2 ) / 3 * 4 + 2 * ( m - n + 1 ) * n * ( n - 1 ); 22 } 23 24 int main () 25 { 26 ll n, m; 27 while ( cin >> n >> m ) 28 { 29 if ( n == 0 && m == 0 ) break; 30 if ( n > m ) swap( n, m ); 31 cout << a( n, m ) + b( n, m ) + d( n, m ) << endl; 32 } 33 return 0; 34 }