zoukankan      html  css  js  c++  java
  • uva 12075

    12075 - Counting Triangles

    Triangles are polygons with three sides and strictly positive area. Lattice triangles are the triangles all whose vertexes have integer coordinates. In this problem you have to find the number of lattice triangles in an M x N grid. For example in a (1 x 2) grid there are 18 different lattice triangles as shown in the picture below:

    epsfbox{p3295.eps}

    Input 

    The input file contains at most 21 sets of inputs.

    Each set of input consists of two integers M and N ( 0 < M, N$ le$1000 ). These two integers denote that you have to count triangles in an(M x N) grid.

    Input is terminated by a case where the value of M and N are zero. This case should not be processed.

    Output 

    For each set of input produce one line of output. This output contains the serial of output followed by the number lattice triangles in the(M x N) grid. You can assume that number of triangles will fit in a 64-bit signed integer.

    Sample Input 

    1 1
    1 2
    0 0
    

    Sample Output 

    Case 1: 4
    Case 2: 18

    大意:

      给出一个 n*m 的网格, 求出这个网格中的三角形的个数.

      这个和上一题有异曲同工之妙....

      minus[i][j] 为矩形尺寸为 i,j 的时候的 (i,j) 对共线点对的贡献.

      则 minus[i][j] = minus[i-1][j] + minus[i][j-1] - minus[i-1][j-1] + gcd(i,j) - 1;(指的是非平行(或垂直)共线,所以要另外计算).

      ans[i][j] 为矩形尺寸为 i,j 的时候, 总的重复点对个数.

      则 ans[i][j] = ans[i-1][j] + ans[i][j-1] - ans[i-1][j-1] + minus[i][j];

      然后答案就出来了.

      

     1 #include<cstdlib>
     2 #include<cstdio>
     3 #include<iostream>
     4 using namespace std;
     5 long long n,m,ans;
     6 long long cond[1500][1500],mit[1500][1500];
     7 int kase;
     8 int gcd(int x,int y){ return !y ? x : gcd(y,x%y); }
     9 long long C(long long x){ return x * (x - 1) * (x-2) / 6; }
    10 void init(){
    11     for(int i = 1; i <= 1000; ++i)
    12         for(int j = 1; j <= 1000; ++j) cond[i][j] = cond[i-1][j] + cond[i][j-1] - cond[i-1][j-1] + gcd(i,j) - 1;
    13     for(int i = 1; i <= 1000; ++i)
    14         for(int j = 1; j <= 1000; ++j) mit[i][j] = mit[i-1][j] + mit[i][j-1] - mit[i-1][j-1] + cond[i][j];
    15 }
    16 int main()
    17 {
    18     freopen("count.in","r",stdin);
    19     freopen("count.out","w",stdout);
    20     init();
    21     while(cin >> n >> m, n+m){
    22         n++, m++;
    23         ans = C(n * m);
    24         ans -= m*C(n) + n*C(m);
    25         ans -= mit[n-1][m-1] * 2;
    26         cout << "Case " << ++kase << ": " << ans << endl;
    27     }
    28     return 0;
    29 }
    View Code
  • 相关阅读:
    HDU
    HDU
    [JSOI2009]计数问题 二维树状数组BZOJ 1452
    THU 上机 最小邮票数 暴力枚举
    SJTU 机试 最小面积子矩阵 压缩+双指针
    SJTU 机试 数学
    Preprefix sum BZOJ 3155 树状数组
    [HNOI2004]树的计数 BZOJ 1211 prufer序列
    HDU 5009
    上下界网络流模型常见解法
  • 原文地址:https://www.cnblogs.com/Mr-ren/p/4227443.html
Copyright © 2011-2022 走看看