Codeforces 579A. Raising Bacteria Solution

 Explain: Here ,we look that n bacteria split in the night.Always time it's split to 2x .

That's means 1*2 >2*2>2*2*2>2*2*2*2>......... if we convert  binary representation

                       10> 100>1000>10000

example: 

input :5

output:2

we can add one bacterium in the box in the first day morning and at the third morning there will be 4 bacteria in the box. Now we put one more resulting 5 in the box. We added 2 bacteria in the process so the answer is 2.

input :8

output:1

we can put one in the first morning and in the 4-th morning there will be 8 in the box. So the answer is 1.

5 's binary representation 101

also 8 s binary representation 1000

we understand that number of 1 (binary representation ) is the minimum number of bacteria we put.

For finding number of 1 we can use __builtin_popcount(x): This function is used to count the number of one’s(set bits) in an integer. 

Solution in C++:

 ///La ilaha illellahu muhammadur rasulullah

///******Bismillahir-Rahmanir-Rahim******///

///AH Tonmoy

///Department of CSE,23rd batch

    ///Islamic University,Bangladesh

    1. #include<bits/stdc++.h>
    2. using namespace std;
    3. int main()
    4. {
    5. int n;
    6. cin>>n;
    7. cout<<__builtin_popcount(n)<<endl;
    8. }

    Another solution:

    1. #include<bits/stdc++.h>
    2. using namespace std;
    3. int main()
    4. {
    5. int n,i,c=0,r;
    6. cin>>n;
    7. while(n>0)
    8. {
    9. c+=n%2;
    10. n/=2;
    11. }
    12. cout<<c<<endl;
    13.  
    14. }






    No comments

    Most View Post

    Recent post

    Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

      Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h ...

    Powered by Blogger.