ZOJ 3647 Gao the Grid dp,思路,格中取同一行的三点,经典 难度:3

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4837

三角形的总数=格子中任取3个点的组合数-同一横行任取3个点数目-同一纵行任取3个点数目-同一斜直线上任取3个点数目

同一横行和同一纵行都好求

同一斜行的距离最远的点必然是一个矩形的两个端点,设矩形的长宽为l,w,中间可以取的点数则是(gcd(l,w)-1),左上角或左下角的起点反而不重要.

能够取到该矩形的可能是 (n-l+1)*(m-w+1),因为左上角作为起点或左下角作为起点都可以,所以斜行的情况是sigma((gcd(l,w)-1)*(n-l+1)*(m-w+1)*2)

比赛时虽然考虑到了矩形但是没有想到让两个端点恰好成为最远的点而是分类统计,最终因为不够精细卡题

#include <cstdio>
#include <iostream>
#include <cstring>
#define clr(x,y) memset(x, y, sizeof x)
#include <cmath>
using namespace std;

typedef long long LL;

LL n, m;

LL cal(LL x)
{
    if (x < 3) return 0;
    return x * (x - 1) * (x - 2) / 6;
}

LL gcd(LL a,LL b)
{
    if(b==0)
        return a;
    else
        return gcd(b,a%b);
}
int main()
{
    while(scanf("%lld%lld", &n, &m) != EOF)
    {
        LL ans = cal((n +1) * (m + 1)) - (m + 1) * cal(n+1) - (n + 1) * cal(m + 1);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
				ans-=2*(gcd(i,j)-1)*(n-i+1)*(m-j+1);

            }
        }
        printf("%lld\n", ans);
    }
    return 0;
}
时间: 2024-10-24 14:39:54

ZOJ 3647 Gao the Grid dp,思路,格中取同一行的三点,经典 难度:3的相关文章

ZOJ 3647 Gao the Grid(居然是暴力)

A n * m grid as follow: 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). Input The input consists of several cases. Each

zoj 3647 Gao the Grid

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4837 先求出从所有点随机找出三个点的组合数,然后去掉共线的,平行好去掉,斜线就需要枚举矩形,也就是枚举两个端点形成向量. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define maxn 1000 5 #define ll long long 6 using

ZOJ 3211 Dream City (J) DP

Dream City Time Limit: 1 Second      Memory Limit: 32768 KB JAVAMAN is visiting Dream City and he sees a yard of gold coin trees. There are n trees in the yard. Let's call them tree 1, tree 2 ...and tree n. At the first day, each tree i has ai coins

ZOJ 3551 Bloodsucker (概率DP)

ZOJ Problem Set - 3551 Bloodsucker Time Limit: 2 Seconds      Memory Limit: 65536 KB In 0th day, there are n-1 people and 1 bloodsucker. Every day, two and only two of them meet. Nothing will happen if they are of the same species, that is, a people

zoj 3781 Paint the Grid Reloaded (比较隐含的最短路)

Paint the Grid Reloaded Time Limit: 2 Seconds      Memory Limit: 65536 KB Leo has a grid with N rows and M columns. All cells are painted with either black or white initially. Two cells A and B are called connected if they share an edge and they are

zoj 3780 Paint the Grid Again (拓扑排序)

Paint the Grid Again Time Limit: 2 Seconds      Memory Limit: 65536 KB Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black or white). Leo has a magical brush which can paint any row with black color, or an

ZOJ 3781 Paint the Grid Reloaded (最短路)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 题意: 在n*m矩阵的图定义连通区域为x值或y值相同且颜色相同的连通,连通具有传递性 每次可以把一个连通区域颜色反转(O变X,X变O) 问把所有块的颜色变为X最小的步数 方法: 很巧妙的最短路问题,先建图,然后以每个顶点为起点,找单源最短路的最大的值(也就是最深的深度),然后这个值取min 建图:相邻的块连边权1的边(即:通过1次反转可以使得两个连通块变为一个连通块

zoj 3791 An Easy Game dp

An Easy Game Time Limit: 2 Seconds      Memory Limit: 65536 KB One day, Edward and Flandre play a game. Flandre will show two 01-strings s1 and s2, the lengths of two strings are n. Then, Edward must move exact k steps. In each step, Edward should ch

ZOJ 3791 An easy game DP+组合数

给定两个01序列,每次操作可以任意改变其中的m个数字 0变 1  1 变 0,正好要变化k次,问有多少种变法 dp模型为dp[i][j],表示进行到第i次变化,A,B序列有j个不同的 变法总和. 循环k次,每次针对m,向那j个不同 分1-j个即可,不过要用到组合数,因为对每个数操作不同都不一样 最后结果就是 dp[k][0] #include <iostream> #include <cstdio> #include <cstring> #include <alg