Editorial
Let represent the dimensions of a rectangle that can be formed using the squares. Given that and , it follows that .
Let's iterate over the smaller side of the rectangle from to . For each , iterate over the second side , from until . In the variable , count the number of such pairs .
cnt = 0; for (i = 1; i <= sqrt(n); i++) for (j = i; i * j <= n; j++) cnt++;
The second loop can be replaced with a formula. Notice that in the loop runs from to . Therefore, during the -th iteration, the value of increases by . The double loop can thus be rewritten as follows:
cnt = 0; for (i = 1; i <= sqrt(n); i++) cnt = cnt + (n / i - i + 1);
Example
Consider the first test case where . The smaller side of the rectangle iterates from to .
For , the second side can take values from to . This gives us rectangles:
For , the second side can take values from to . This gives us rectangles:
In total, for , there are possible rectangles.
Algorithm realization
Read the input value of .
scanf("%d", &n);
Compute the answer.
cnt = 0; for (i = 1; i <= sqrt(n); i++) cnt = cnt + (n / i – i + 1);
Print the answer.
printf("%lld\n", cnt);
Python realization
import math
Read the input value of .
n = int(input())
Compute the answer.
cnt = 0 for i in range(1, int(math.sqrt(n)) + 1): cnt += (n // i - i + 1)
Print the answer.
print(cnt)