ZOJ 3647 Gao the Grid

题目链接 : ZOJ3647

Gao the Grid

 


 

Time Limit: 2 Seconds                                     Memory Limit: 65536 KB                            

 


 

A n * m grid as follow:

a n*m grid(n=4,m=3)

Count the number of triangles, three of whose vertice must be grid-points.
Note that the three vertice of the triangle must not be in a line(the right picture is not a triangle).

a triangle not a triangle

Input

The input consists of several cases. Each case consists of two positive integers n and m (1 ≤ n, m ≤ 1000).

Output

For each case, output the total number of triangle.

Sample Input
1 1
2 2
Sample Output
4
76
hint

hint for 2nd case: C(9, 3) - 8 = 76

 


                            Author: WU, Yingxin

 

啊 这题挂了好多次 组合数学确实不好搞

 

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<queue>
using namespace std;


int gcd(int a,int b)
{
    int c=a%b;
    while(c)
    {
        a=b;b=c;
        c=a%b;
    }
    return b;
}
long long C(int a)
{
    long long temp=a;
    return temp*(temp-1)*(temp-2)/6;
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)==2)
    {
        m++;n++;
        long long ans=C(n*m)-C(n)*m-C(m)*n,temp;
        m--;n--;
        for(int i=2;i<=n;i++)
            for(int j=2;j<=m;j++)
            {
                temp=gcd(i,j);
                ans-=(temp-1)*(n-i+1)*(m-j+1)*2;
            }
        printf("%lld\n",ans);
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值