BZOJ3505 CQOI2014 数三角形 组合数学

题意:给定一个N*M的网格,求有多少个不同的三角形,顶点在交点上。
题解:首先找出所有三个点的组合,然后删除同行,同列,同对角线(横纵坐标的gcd相同)

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long

const int MAXN=1800+2;
int N,M;
ll C[MAXN*MAXN][4],ans;

ll gcd(ll a,ll b){ return !b?a:gcd(b,a%b);}

int main(){
    cin >> N >> M,N++,M++;

    C[0][0]=1;
    for(int i=1;i<=N*M;i++){
        C[i][0]=1;
        for(int j=1;j<=3;j++) C[i][j]=C[i-1][j-1]+C[i-1][j];
    }

    ans=C[N*M][3]-N*C[M][3]-M*C[N][3];
    for(int i=1;i<N;i++)
        for(int j=1;j<M;j++){
            ll t=gcd(i,j)+1;
            if(t>2) ans-=(t-2)*2*(N-i)*(M-j);
        }
    cout << ans << endl;

    return 0;
}
View Code

 

posted @ 2017-02-26 02:10  WDZRMPCBIT  阅读(120)  评论(0编辑  收藏  举报