三道组合题

uva11538

题意:问一个n*m的棋盘 有多少种方法可以放置两个可以相互攻击的皇后。

讨论下三种情况 横竖斜。  前n项平方和公式: n*(n+1)*(2*n+1)/6

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <string>
#include <iostream>
#include <map>
#include <cstdlib>
#include <list>
#include <set>
#include <queue>
#include <stack>
#include<math.h>
using namespace std;

typedef long long LL;

int main()
{
    LL  n, m;
    while (cin >> n >> m,n||m){
        if (n > m) swap(n, m);
        LL ans = n * m *(m - 1) + n*(n - 1)*m;
        LL ans1 = n*(n - 1) * (2 * n - 1) / 3 - n*(n - 1) + (m - n + 1)*n*(n - 1);
        LL ans3 = ans + ans1 * 2;
        cout << ans3 << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/yigexigua/p/4018374.html