ZOJ 3647 Gao the Grid (计数 + pick定理)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3647

 

题意:给一个n * m的矩形,求出三个点都落在该矩形格点上的三角形的个数

 

思路:先算出任取三点的数目,再减去两种共线的情况:

1.三个点落在同一行或者同一列

2.三个点落在斜线上,可以枚举矩形,然后固定原点和(i, j)点(相当于这条线段的始末点)再用pick定理算出这条线段上整点数目,统计和这条线段平行的线段在整个矩形中出现的次数即可

 

 

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <utility>
#include <functional>
#include <string>
#include <set>
#include <map>
#pragma comment(linker, "/STACK:102400000,102400000")

using namespace std;

const int maxn = 50010;

typedef long long ll;


int n, m;

int gcd(int x, int y)
{
    return y ? gcd(y, x % y) : x;
}

ll C(ll x)
{
    if (x <= 2) return 0;
    return x * (x - 1) * (x - 2) / 6;
}


int main()
{
    while (cin >> n >> m)
    {
        ll ans = C((n + 1) * (m + 1));
        ans -= (m + 1) * C(n + 1) + (n + 1) * C(m + 1);
        for (int i = 2; i <= n; i++)
        {
            for (int j = 2; j <= m; j++)
            {
                int t = gcd(i, j) -  1;
                ll ttt = t * (m + 1 - j) * (n + 1 - i);
                ans -= ttt * 2;
            }
        }
        cout << ans << endl;
    }
    return 0;
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值