Last active
December 19, 2015 01:09
-
-
Save cocodrips/5873561 to your computer and use it in GitHub Desktop.
SRM 146 DIV1 300 長方形の中に長方形(正方形を除く)はいくつある?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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