- Practice
- Mathematics
- Fundamentals
- Army Game
- Discussions

# Army Game

# Army Game

nimon + 17 comments Nice one liner

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

jayshree29 + 0 comments [deleted]XiaoHan + 0 comments Wonderful!

vivektalwar13071 + 0 comments [deleted]shalatahammo + 0 comments [deleted]kimish_r + 0 comments [deleted]vbn_1995 + 1 comment Could you please explain the logic behind this ? i am getting confused totally

mbarteau + 5 comments n is the number of columns, n%2 will give you either 1 or 0 depending on if it is divisible by 2 or not. The same will be done with the rows using m and m%2.

This gives us (n+n%2) (which we can think of as N from here on out) is even, and (m+m%2) (which we can think of as M) is also even, and although it might be larger than the original n*m board, it makes it easier to calculate the number of bases needed since if we had an odd number we would have to add 1 base on the last row or column.

With our new N*M board, it is easy to solve since each base can fit 4 squares, 2 from the N and 2 from the M. So we do N*M/4 to get the number of bases needed.

vbn_1995 + 0 comments i understood it afterwards but thanks for the explanation anyway :)

csepratiti + 0 comments is the main idea here to make a fit of 4 sqaures at go which will reduce the amount of supplies as whole?

691abhayrawat + 0 comments sir, i didn't understand yet, The question as well as your answer..

691abhayrawat + 1 comment well, it's my first time and i m a begginer programmer , but could you please explain what exactly the quesiton is

Sojal + 3 comments If you haven't solved it yet - the key here is: a supply drop can cover 4 blocks. Therefore if the grid is 2x2, 1 suppy drop is enough. But if the grid is 3x2?

1 suppy drop will cover 2x2 portion of the grid but still there is 1x2 part and you need a supply drop there anyway. Same as how many you would need in a 4x2 grid.

Similarly if the region was 3x3 you would need as much as drops as you would need in a 4x4 grid. Otherwise some blocks would be left empty.

So for simplicity of calculation we increment the odd integer to it's nearest bigger even (yes, that means odd+1) and then multiply n and m and divide it by 4. That's how many supply drops we must need to cover all the blocks.sankireddyvinee1 + 0 comments good....

aaronshaverpdx + 0 comments Perfect

PAL_RAJAT1997 + 0 comments wow!!!!!!!!!!! Amazing

poptifreaadrian + 0 comments Thank you !

Rugrat + 0 comments To whomever did it: Downvoting those guys asking for explanation is a dick move. thank you for your attention.

[DELETED] + 0 comments [deleted]DrManhattan + 1 comment A more intutive ( though lenthier) way is as follows

int main(){ int l,b; cin>>l>>b; int sum4=0; int sum2=0; int tot=0;// this keeps the count of the number of supplies int prod=l*b; // count the number of 4 blocks ( the largest range i.e 4 blocks = 1 supply ) sum4=(l/2) *( b/2);// this gives the number of supplies for 4 unit blocks tot+=sum4;//add the no. of supplies till now prod-=sum4*4; // deduct the number of blocks taken out sum2=prod/2; // count the number of 2 blocks ( the second largest range i.e 2 blocks = 1 supply ) tot+=sum2; prod-=sum2*2; // deduct the number of blocks taken out tot+=prod; // finally add the no of 1's remaining ( 1 block = 1 supply) cout<<tot; // finally print the total number of supplies return 0; }

tordjarv + 0 comments In what way is this a more intuitive solution than just printing (n/2+n%2)* (m/2+m%2), that is realizing that at maximum a package can supply four bases and if n or m is odd we have to add one package more in either direction.

nitishagupta_191 + 0 comments [deleted]xorates + 0 comments Cool. Yours is a simplified version of mine! :o

n+=1 if n%2 else 0; m+=1 if m%2 else 0; print((n*m)//4);

inficreator + 0 comments really great

gautamxdeepak + 0 comments yo bro.

karthi_16p141 + 0 comments NICE

Gupta31prashant + 0 comments great job

vishalvutukuri + 0 comments great one!

UselesssCat + 0 comments In php, this passes al testcases:

`return ceil(`$n / 2) * ceil($`m / 2);`

(I do not understand why the quotes magically appear on the line above.)

q1a2z3 + 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.

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

Lidzikr + 0 comments i got this

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

bhaveshchetwani + 1 comment Java - Best answer : ((n + n%2) * (m + m%2))/4

gaston_b + 0 comments `System.out.println((++n/2) * (++m/2));`

layman_brother + 1 comment 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; }

sriram_kamma + 1 comment can u pls explain it!!

layman_brother + 0 comments no. of rows and colums either will be in multiple of 4 or not. so here, I am trying to make group of 4 blocks , two from rows and two from column, and the rest two or four in the last will be considered... u can make simple test cases...

klssh3126 + 0 comments Hi, This is my code

int gameWithCells(int n, int m) { if(n%2==1) n++; if(m%2==1) m++; return (n/2)*(m/2); }

olegcherednik + 0 comments static int gameWithCells(int n, int m) { return (int)Math.ceil(n / 2.) * (int)Math.ceil(m / 2.); }

Vishu26 + 0 comments Imagined the convolutional layer of a CNN. Kernel size = [2,2] and strides = [2,2] and zero padding to "same".

akashbhadouria85 + 1 comment My approach to solve this question was following : total number of blocks will be m*n,so if we place every pakage in the vertex shared by 4 squares,so we have to place m*n/4 pakages.But when i compiled the code it's not satisfying most of the test cases.Why is it so ?

Mohit_Yadav_389 + 0 comments Try this:

printf("%d",(n/2)*(m/2) + (m*n-4*(n/2)*(m/2))%2 +(m*n-4*(n/2)*(m/2))/2);

Sort 62 Discussions, By:

Please Login in order to post a comment