zoj 3647 组合数学

来源:互联网 发布:马云在淘宝怎么赚钱 编辑:程序博客网 时间:2024/06/05 18:34

给个n*m的方格,问有多少三角形,共线的不算。

共有 (n+1)*(m+1) 个点,设为 t 。 从这里面选出3 个点,共有C(t,3)种,在水平上共线: ( (m+1)*fun(n+1,3) ) 在竖直上共线的有 ( (n+1)*fun(m+1,3) ),

对于倾斜的情况,先枚举两端的端点,如图,在一个6*6的方格中选4*4的两个端点,其中可构成三点花线的另一点的个数为最大公约数gcd(4,4) -1.如图中的三个点,然后用乘于剩下的倍数(6-4+1*6-4+1)(为6*6方格中4*4矩形的个数),再乘于2(倾斜时有右上,右下两种情况)


#include<cstdio>#include<cstring>#include<queue>#include<iostream>using namespace std;#define FF  freopen("Input.txt","r",stdin)#define mem(x,y) memset(x,y,sizeof(x))#define ll long longint gcd(int a,int b){    int c=a%b;    while(c)    {        a=b;        b=c;        c=a%b;    }    return b;}//ll fun(ll n)//{//    if(n<3) return 0;//    return n*(n-1)*(n-2)/6;//}ll fun(ll n,ll m){    if(n<m) return 0;    if(m>n/2) m=n-m;    ll z=1,f=1,i;    for(i=1;i<=m;i++)    {        z=z*(n-i+1);        f=f*i;    }    return z/f;}int main(){    int n,m,i,j;    while(~scanf("%d%d",&n,&m))    {        ll ans=fun( (n+1)*(m+1),3 );        for(i=2;i<=n;i++)          for(j=2;j<=m;j++)            ans-=(ll)( gcd(i,j)-1 )*(n-i+1)*(m-j+1)*2;        ans-=( (m+1)*fun(n+1,3) );        ans-=( (n+1)*fun(m+1,3) );        printf("%lld\n",ans);    }    return 0;}


原创粉丝点击