• UVA 12508 Triangles in the Grid


    UVA_12508

        这个题目的一个坎就是要想到去枚举三角形的外接矩形,如果想到这一点的话剩下的思路就不难想了,分情况计算出各种三角形(当然这些三角形的外接矩形必须是枚举的这个矩形)的数量然后再乘以大矩形中有多少个这样的外接矩形,这样一种外接矩形的情况就算是算完了。所有外接矩形的情况累加起来就是最终的结果了。

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    typedef long long LL;
    int N, M, A, B;
    LL deal(int limit)
    {
        LL ans = 0;
        for(int n = 1; n <= N; n ++)
            for(int m = 1; m <= M; m ++)
            {
                LL cnt = 0;
                int y1, y2;
                if(m * n <= limit) cnt += 2 * (m - 1 + n - 1);
                for(int x = 0; x <= n; x ++)
                {
                    y2 = (m * x + limit) / n;
                    if(y2 > m) y2 = m;
                    int t = m * x - limit;
                    if(t <= 0) y1 = 0;
                    else y1 = (t - 1) / n + 1;
                    if(y1 <= y2) cnt += 2 * (y2 - y1 + 1);
                }
                for(int x = 1; x < n; x ++)
                {
                    y2 = (m * n + limit) / x;
                    if(y2 >= m) y2 = m - 1;
                    int t = m * n - limit;
                    if(t <= 0) y1 = 1;
                    else y1 = (t - 1) / x + 1;
                    if(y1 <= y2) cnt += 4 * (y2 - y1 + 1);
                }
                ans += cnt * (N - n + 1) * (M - m + 1);
            }
        return ans;
    }
    void solve()
    {
        A <<= 1, B <<= 1;
        if(A == 0) A = 1;
        printf("%lld\n", deal(B) - deal(A - 1));
    }
    int main()
    {
        int t;
        scanf("%d", &t);
        while(t --)
        {
            scanf("%d%d%d%d", &N, &M, &A, &B);
            if(N > M) std::swap(N, M);
            solve();
        }
        return 0;
    }
  • 相关阅读:
    不定方程(Exgcd)
    [模板]乘法逆元
    STL-Deque(双端队列)与单调队列的实现
    最优得分 score
    摆书 book
    [模板]树链剖分
    [模板]Splay
    NOIP2013 货车运输
    Java的类类型和类的动态加载
    Java:搜索特定后缀名的文件
  • 原文地址:https://www.cnblogs.com/staginner/p/2743086.html
Copyright © 2020-2023  润新知