×

# TRISQ - Editorial

Author: Devendra Agarwal
Tester: Anudeep Nekkanti
Editorialist: Amit Pandey

Cakewalk.

# PREREQUISITES:

Basic geometry, recursion.

# PROBLEM:

Find the maximum number of squares of size $2\times2$ that can be fitted in a right angled isosceles triangle of base $B$. All sides of the square must be parallel to both base and height of the isosceles triangle.

# QUICK EXPLANATION:

As $B<=1000$, we can pre-compute the output for all the test cases, and report the answer in $O(1)$ time for each of the query.

# EXPLANATION:

First consider the right angle triangle $\Delta XYZ$, where $YZ$ is the base of triangle. Suppose length of the base is $B$. If we consider the position of first square with the vertex $Y$, we will have $(B/2 - 1)$ squares in the base, and we will be left with another isosceles right angle triangle having base length $(B-2)$.

Let $f(B)$ = Number of squares which can be fitted in triangle having base length $B$.

$f(B) = (\frac{B}{2} -1) + f(B-2)$

The given time limit is sufficient even if we calculate $f(B$) using the given recursion, and use memoization. Later we can answer each query in $O(1)$ time. We can do it for even and odd numbers separately with the base case $f(1) = f(2) = f(3) = 0$.

The given recursion can be solved in following manner.

$$f(B) = \frac{B-2}{2} + F(B-2) \\\ = \frac{B-2}{2} + \frac{B-4}{2} + F(B-4) \\\ = \frac{B-2}{2}+ \frac{B-4}{2} + \frac{B-6}{2} + F(B-6)$$

With conditions, $f(1) = f(2) = 0$

$f(B) = \frac{B}{2} + (\frac{B}{2} - 1) + (\frac{B}{2} -2) + \cdots + 1$.

$f(B)$ = Sum of first $\frac{B}{2}$ natural numbers.

Lets call $M = \frac{B}{2}$
$f(B) = \frac {M \times (M-1)}{2}$

You can notice that answer for $2N$ and $2N+1$ will be the same.

# Solution:

Setter's solution can be found here
Tester's solution can be found here

This question is marked "community wiki".

25911522
accept rate: 0%

19.8k350498541

"we will have (B/2−1) squares in the base, and we will be left with another isosceles right angle triangle having base length (B−2)." Where does this thing come from... please help me ....

(05 Jun '15, 17:17)

 3 My solution long long int t1,t2,b; t2=b/2; t1=t2*t2; t1=t1-t2; t1=t1/2; cout<
 5 It can be solved in a more simple way. Consider you need to fill the isosceles triangle with 1x1 squares. Now, consider a square of side B. The number of 1x1 squares you can put in is clearly BxB, and also the number of 1x1 squares you can put in an isosceles triangle will be (BxB - B)/2 ie we remove the squares on the diagonal and half the remaining squares . Now you need to scale the 1x1 to 2x2, so you simply divide B by 2, and use (BxB - B)/2. My Solution I know the proof is more intuitive rather than rigorous, would be happy to know if there is some fallacy, or some rigourous way of proving it. answered 16 Feb '15, 01:01 3★mjnovice 134●3●5●13 accept rate: 0%
 1 My approach was wrong but it got AC anyway. Observing the test cases the answer for consecutive size of the base is - 0 0 0 1 1 3 3 6 6 10 10... which is nothing but the series of sum of n integers with all the sums repeating twice, except for first three 0s. After pre-computing this for 10^4 each query can be answered in O(1). Perhaps the problem could have been presented with less and randomized test cases to prevent the use of such approach. answered 16 Feb '15, 01:23 45●3 accept rate: 0%
 1 It can also be solved as through the following code :-->> int N=(B/2)-1; print((N*(N+1))/2); answered 11 Jul '15, 17:05 11●1 accept rate: 0%
 0 My approach: A=(B[i]/2)*(B[i]/2); A-=(B[i]/2); A-=((((B[i]/2)*(B[i]/2))/2)-(B[i]/4)); cout << A << endl; Maybe it can be simplified further. answered 16 Oct '15, 18:40 31●2 accept rate: 0%
 0 how u have generated a general formula for this problem??? plz help.... answered 17 Oct '15, 05:55 1★vk34 1 accept rate: 0%
 0 Firstly, divide 'b' or 'n' by 2 so we can deal with 1x1 squares. At the base, n-1 squares can be fit and it decreases by 1 as we go upwards. Hence the total number of squares is the sum=(n-1)+(n-2)+...+2+1. Summation of +ve: n is added n-1 times. Therefore, sum of n's is n(n-1). Summation of -ve: Summation of series of natural numbers is '(N)(N+1)/2'. Here N=n-1 so summation is n(n-1)/2. Subtracting -ve from +ve, we get: n(n-1)/2 or b(b-1)/2 answered 19 Jun '16, 20:24 3★cyberneo 1 accept rate: 0%
 0 Nothing Understood , Very Bad Explanation :( answered 17 May '17, 22:26 0★joy59 1 accept rate: 0%
 0 Isn't sum of the first M natural numbers M*(M+1)/2 ? answered 23 Jun '17, 19:14 58●5 accept rate: 5%
 0 For people willing to know where this statement came from: We will have (B/2−1) squares in the base, and we will be left with another isosceles right angle triangle having base length (B−2). You can deduce this simply by using simple trigonometry. I was also curious to find the reason behind this and then got to this approach. Please correct me if I'm wrong or if there's a better approach. If you can, try visualizing this on a notebook. Consider in triangle XYZ, YZ is the base with Z as the right-angled vertex. Thus, YX is the hypotenuse. Let the length of base YZ = B. SInce, it's an isosceles triangle length of YX is also B. Thus, the angle XYZ is 45 degrees (tan(XYZ) = B/B = 1). If we start filling squares of side 2 units from vertex Z, we can only fill them until the distance between base and the hypotenuse becomes less than 2. Since, the corresponding angle is 45 degrees, the distance from Y to this point will also be 2. Hence, we will not be able to place 1 square of side 2 here. The total number of squares that could be filled thus B/2 - 1. B/2 since there are of side 2 units. Then we are left with another isosceles triangle above these filled squares with base B-2 that is simply the sum of the length of upper sides of all squares (2*(B/2 - 1) = B - 2). Hope I was clear with the explanation. link This answer is marked "community wiki". answered 19 Aug '17, 17:32 1 accept rate: 0%

Why did I get a WA?

Why did I get WA??

# include<math.h>

int T, B, n, x; using namespace std ;

int main(){

scanf("%d",&T); while(T--) { scanf("%d",&B);

        if (B>3){
if ((B%2)!=0)
B--;
x= (B*B)/4;
n= ( (x) - sqrt(x) )/2;
printf("%d",n);

}

else{
n=0;
printf("%d",n);
}
}


return 0; }

1
accept rate: 0%

 0 Another alternative way of doing it is arithmetic series: where n = int(b/2-1) and ![alt text][2] and A1 = 1 and An = n Example: B = 20, n =(20/2-1) = 9, 9(1+9)/2 = 45 answered 16 Mar '18, 02:36 0★rafferz 1 accept rate: 0%
 0 Recursive function for this problem is f(x) = 1 + 2*f(x-2) - f(x-4) answered 03 Apr '18, 15:38 11●1 accept rate: 0%
 0 F(B)=B/2+(B/2-1)+(B/2-2)+.....+1 can anyone plz tell me from where the 1st B/2 term come? answered 22 Jun '18, 13:53 1 accept rate: 0%
 0 @subarna_08. Correct summation is : ((B/2) - 1) + ((B/2) - 2) + ... + 1 = Sum of first ((B/2) - 1) positive integers. Hence the correct answer is: ( ((B/2) - 1)(B/2) )/2. (Sum of first n positive integers is n(n+1)/2) answered 14 Oct '18, 15:29 0★zangliu 1 accept rate: 0%
 0 Can anyone tell me why the code with precompute is throwing the error NZEC, while without precompute it is working correctly in Java? Code: With Precompute: https://www.codechef.com/viewsolution/21643739 WithOUT precompute: https://www.codechef.com/viewsolution/21644055 answered 18 Nov '18, 21:56 1 accept rate: 0%
 0 Java Recursive solution: import java.util.*; class Solution { public static int derive(int n) { if(n==1 ||n==2 ||n==3) return 0; return (n/2-1)+derive(n-2); } public static void main(String []args) { Scanner sc=new Scanner (System.in); int item=sc.nextInt(); while(item!=0) { item--; int n=sc.nextInt(); int x=derive(n); System.out.println(x); } }  } answered 27 Dec '18, 14:25 1 accept rate: 0%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×1,652
×637
×348
×109
×3

question asked: 09 Feb '15, 19:55

question was seen: 13,660 times

last updated: 27 Dec '18, 14:25