Skip to content

Instantly share code, notes, and snippets.

@cocodrips
Last active December 19, 2015 01:09
Show Gist options
  • Save cocodrips/5873561 to your computer and use it in GitHub Desktop.
Save cocodrips/5873561 to your computer and use it in GitHub Desktop.
SRM 146 DIV1 300 長方形の中に長方形(正方形を除く)はいくつある?
public long countRectangles(int width, int height) {
long count = 0;
count = calcCombination(width+1, 2)*calcCombination(height+1, 2);
int short_side = Math.min(width, height);
for (int i = 0; i <= short_side; i++) {
count -=(width - i) * (height - i);
}
return count;
}
long calcCombination(int n, int m){
if(n < m || m < 0) {
return 0;
}
long c = 1;
m = (n - m < m ? n - m : m);
for(int ns = n - m + 1, ms = 1; ms <= m; ns ++, ms ++) {
c *= ns;
c /= ms;
}
return c;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment