Codeforces and Polygon may be unavailable from December 6, 19:00 (UTC) to December 6, 21:00 (UTC) due to technical maintenance. ×

HACKERRANK hour-of-code-round-ii

Revision en2, by saisumit, 2017-02-14 00:52:31

Hello people , I am stuck at this problem SITTING ARRANGEMENTS , i read the editorial but couldn't figure why the solution works, if anyone can provide an explanation for the solution , it would be great. Thanks in advance :)

This is the author's solution


#include<bits/stdc++.h> using namespace std; typedef long long int ll; ll solve(ll n,ll m) { if(n==0||m==0) return 0; else if(n%2==0&&m%2==0) return solve(n/2,m/2); // Halving both dimensions doesn't change the number of tiles else if(n%2==0&&m%2==1) return (n+solve(n/ 2,m/ 2));// Use a row of 1x1 tiles else if(n%2==1&&m%2==0) return (m+solve(n/ 2,m/ 2));// Use a column of 1x1 tiles else return (n+m-1+solve(n/2,m/2)); //ROW OR COLUMN (WHICHEVER OVERLAP) } int main() { ll t; cin>>t; while(t--) { ll n,m,i,j,p,q,r; cin>>n>>m; cout<<solve(n,m)<<endl; } }
Tags combinations, maths, recursion

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English saisumit 2017-02-14 00:55:51 6 Tiny change: 'xplanation for the s' -> 'xplanation/proof for the s'
en2 English saisumit 2017-02-14 00:52:31 745
en1 English saisumit 2017-02-14 00:49:49 353 Initial revision (published)