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 namespace std;
 7
 8 ll c[maxn][maxn];
 9 int n,m;
10 ll gcd(ll a,ll b)
11 {
12     if(b==0) return a;
13     else return gcd(b,a%b);
14 }
15
16 ll cnr(int n,int r)
17 {
18     if(n<r)return 0;
19     if(n-r<r)r=n-r;
20     int i,j;
21     ll ret=1;
22     for(i=0,j=1;i<r;i++)
23     {
24         ret*=(n-i);
25         for(;j<=r&&ret%j==0;j++)ret/=j;
26     }
27     return ret;
28 }
29
30
31 int main()
32 {
33     while(scanf("%d%d",&n,&m)!=EOF)
34     {
35         ll ans=cnr((n+1)*(m+1),3);
36         if(n+1>=3)
37         ans-=(m+1)*cnr(n+1,3);
38         if(m+1>=3)
39         ans-=(n+1)*cnr(m+1,3);
40         for(int i=2; i<=n; i++)
41         {
42             for(int j=2; j<=m; j++)
43             {
44                 if(gcd(i,j)-1)
45                 {
46                     ans-=(gcd(i,j)-1)*(n-i+1)*(m-j+1)*2;
47                 }
48             }
49         }
50         printf("%lld\n",ans);
51     }
52     return 0;
53 }

时间: 2024-10-14 13:35:54

zoj 3647 Gao the Grid的相关文章

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 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),因为左上角作为起点或左下

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 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 3780 Paint the Grid Again(topsort)

ZOJ Problem Set - 3780 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

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 3647 智商题

此题就是求格点中三角形的个数. 就是找出三点不共线的个数. n*m的矩形中有(n+1)*(m+1)个格点. 选出三个点的总个数为:C((n+1)*(m+1),3). 减掉共线的情况就是答案了. 首先是水平和垂直共线的情况:C(n+1,3)*(m+1)+C(m+1,3)*(n+1); 然后斜的共线的情况就是枚举矩形. 斜着共线的三点用枚举法n*m的矩形,对角两个点中间共有gcd(m,n)-1个点,两条对角线,所以数量*2,大矩形里共有(N-n+1)*(M-m+1)个的矩形,一并去除 1 #incl

ZOJ 3781 Paint the Grid Reloaded(BFS)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 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 in