zoj3647(组合数学)

题意:n*m的矩阵任选三个点,可以有多少种不同的三角形。


解法:组合数学C((n+1)*(m+1),3)是所有三个点的情况。然后在减掉共线的。共线的分为两种:

         1、共横线或竖线:C(n+1,3)*(m+1)+C(m+1,3)*(n+1);

         2,斜线的:这个要枚举矩形,然后三个点有两个取矩形的对角线,另一点枚举(对角线上的整数点个数是gcd(i,j)+1),这样可以做到不重复计算。


代码:

/******************************************************
* author:xiefubao
*******************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <string.h>
//freopen ("in.txt" , "r" , stdin);
using namespace std;

#define eps 1e-8
#define zero(_) (abs(_)<=eps)
const double pi=acos(-1.0);
typedef long long LL;
const int Max=10100;
const int INF=1000000007;


LL C3(LL n)
{
    return n*(n-1)*(n-2)/6;
}
LL C2(LL n)
{
    return n*(n-1)/2;
}
int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}

int main()
{
  LL n,m;
  while(scanf("%lld%lld",&n,&m)==2)
  {
      int grid=(n+1)*(m+1);
    LL ans=C3(grid);
    ans-=C3(n+1)*(m+1)+C3(m+1)*(n+1);
    for(int i=2;i<=n;i++)
        for(int j=2;j<=m;j++)
        ans-=(n-i+1)*(m-j+1)*(gcd(i,j)-1)*2;
    cout<<ans<<endl;
  }
   return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值