- Practice
- Mathematics
- Fundamentals
- Army Game
- Discussions

# Army Game

# Army Game

+ 29 comments Nice one liner

print (n+n%2)*(m+m%2)/4

+ 0 comments I used the fact that m and n are integers to do this approach:

cout<<((n+1)/2)*((m+1)/2);

If n is even, then it will round down (Example: n=4; n+1==5; (n+1)/2==2*.5*; Since it is an integer, the .5 is removed. (n+1)/2==2)

If n is odd, then it simply acts as though it were a slightly larger graph with an even number of rows.

+ 2 comments int main(){ int n; int m; cin >> n >> m; if(n<=2 && m<=2) cout<<"1"; else { cout<<((n%2)+(n/2))*((m/2)+(m%2))<<endl; } return 0; }

+ 0 comments *One liner for c++*int gameWithCells(int n, int m) { return ceil(n/2.0)*ceil(m/2.0); }

+ 0 comments [] denotes base and - denotes drop

First, let's assume when a number of rows is just 1.

If we have 1 base, we will require 1 drop.

If We have 2 bases. [][] We can drop in between them. Hence we will need only 1 drop. []-[]

Now if we have 3 base []-[]-[] then we will require 2 drop to cover all the bases.

If we have 4 base []-[][]-[] only 2 drops can cover all the bases.

We can see if the number of the base is n then we need (n+1)/2 in integer numbers of drops. Since (1+1)/2 = 1, (2+1)/2 = 1, etc.

So, we have the formula for 1 row i.e. (m+1)/2

now if we have 2 rows, then we can shift down the drops to place it in the corner of 4 bases and it will be the same as it was for 1 row

And if we notice as we did for columns, we will find out it follows the same pattern. Hence we just need to multiply the output for coulmn with (n+1)/2

So, result = ((m+1)/2)*((n+1)/2);

Sort 170 Discussions, By:

Please Login in order to post a comment