Sunday, August 1, 2010

Cutting Blocks








Niloy is a cool mathematician and
is good at solving puzzles.He has been given
a puzzle
which he solves very quickly but he wants to check his solution. Now
your task
is to find the answer so that Niloy can match his answer.
Here is the puzzle :)
Given a block of W weight you have to cut the
block
into blocks having a weight of K or less. Each block can be a cut only
if its
weight is greator than the minimum weight k,
such
that it results in blocks with minimal weight difference. We can further
cut
the newer blocks but we can apply only one cut to a particular block.
The cuts
are made such that we get blocks of integral weight. K and w
are both integer.



Input 



n - no of cases
w
( integer ) - weight of original block
K  (
integer ) -  maximum weight of final blocks to be formed
2 < = W  <= 10000
1 < = K


Output 



no of
final blocks formed



Sample input 



5
16 1
4 2
16 2
32 4
11 2


Sample Output 



16
2
8
8
7

#include<iostream>
using namespace std;
int main()
{
int fun(int a,int b);
int test,num1,num2,count;
cin>>test;
     while(test>0)
{
cin>>num1;
cin>>num2;
          count=fun(num1,num2);
      
   cout<<count<<endl;
      
   test--;
    
}
return 0;
}
int fun(int a,int b)
{
if(a<=b)
     {return 1;}
    
else
     {
   
      return fun(a/2,b)+fun(a-(a/2),b);
    }
}

1 comment: