Topcoder SRM146 Div1 Level 1 RectangularGrid

解法


数える長方形の幅をw,高さをhとすると,
その大きさの長方形の個数は(width - w + 1) * (height - h + 1)
これを2重ループで正方形でないもの(w != h)を数えればいい

コード


#include <bits/stdc++.h>
#include <stdint.h>
#include <sys/time.h>

class RectangularGrid {
public:
  long countRectangles(int width, int height) {
    long res = 0;

    for(int w = 1; w <= width; ++w) {
      for(int h = 1; h <= height; ++h) {
        if( w != h ) {
          res += (width - w + 1) * (height - h + 1);
        }
      }
    }

    return res;
  }
};