CIELRCPT - Editorial

Convert into binary and count the number of one’s . That is the answer

4 Likes

#include<stdio.h>

int main()
{
short unsigned int t,i,n;
long unsigned int p;
scanf("%u",&t);
for(i=0;i<t;i++)
{
scanf("%ld",&p);
n = p/2048 + (p%2048)/1024 + ((p%2048)%1024)/512 + (((p%2048)%1024)%512)/256 + ((((p%2048)%1024)%512)%256)/128 + (((((p%2048)%1024)%512)%256)%128)/64 + ((((((p%2048)%1024)%512)%256)%128)%64)/32 + (((((((p%2048)%1024)%512)%256)%128)%64)%32)/16 + ((((((((p%2048)%1024)%512)%256)%128)%64)%32)%16)/8 + (((((((((p%2048)%1024)%512)%256)%128)%64)%32)%16)%8)/4 + ((((((((((p%2048)%1024)%512)%256)%128)%64)%32)%16)%8)%4)/2 + ((((((((((p%2048)%1024)%512)%256)%128)%64)%32)%16)%8)%4)%2;
printf("%u\n",n);
}
return 0;
} 

runtime error… please help !!

1 Like

Write N as Q * 2048 + R, where Q and R are non-negative integers and R < 2048. Then the answer is Q + bitCount®, where bitCount(X) is the number of ones in binary representation of X.

This is my doubt as if N is less than 2048, we cant express the same as Q* 2048+ R as we are told that Q and R are integers( not a fraction ).

how to solve this problem in easy way please tell me…

can somebody please help me with my code here is the link : NovXIt - Online C++ Compiler & Debugging Tool - Ideone.com

i am getting wrong answer .

Why Isn’t THis Approach Working ??

    #include <iostream>
#include <cmath>
using namespace std;
typedef long long int ll;
int main() {
	ll t;
	cin>>t;
	while(t--)
	{
	    ll ans=0,p,lpow;
	    cin>>p;
	    while(p>0)
	    {
	        
	        lpow = log2(p);
	        
	        p-=pow(2,lpow);
	        ans++;
	       
	    }
	    cout<<ans<<endl;
	}
	return 0;
}

Hi,

i have tried to solve using recursion

code follows,but not being accepted as solution

please help,thanks

int n,p,divi,rem,t;

int calc(int num){

if (num == 0)
	return 0;

divi = num / 2048;

rem = num % 2048;

if (divi > 0)
	t = divi + calc(rem);
else
	t = calc(2 * rem);

return t;

}

int main(){

scanf("%d", &n);

for (int i = 0; i < n; i++){
	scanf("%d", &p);
	printf("%d \n", calc(p));
}

return 0;

}

#include<stdio.h>
int main()
{
int t,p,count,key;
scanf("%d",&t);
while(t–)
{
scanf("%d",&p);
if((p%2048)==0)
{
count=p/2048;
printf("%d",count);
printf("\n");
}
else
{
count=count_1§;
if(p/2048>1)
{
key=p/2048;
count=count+(key-1);
printf("%d",count);
printf("\n");
}
else
{
printf("%d",count);
printf("\n");
}
}
}
return 0;
}
int count_1(int n)
{
int ctd;
while(n)
{
n=n&(n-1);
ctd++;
}
return ctd;
}
it satisfy every case but during submission wrong answer

easiest way to solve the problem

import java.util.*;

import java.lang.*;

import java.io.*;

class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{

	Scanner obj = new Scanner(System.in);
	int a[] = new int[12];
	
	for(int i=0;i<=11;i++){
	    a[i] = (int) Math.pow(2,i);
	}
	
	int n = obj.nextInt();
                          
	for(int i=1;i<=n;i++){
                
	    int p = obj.nextInt();
               
	    int k=0,l=0;                
                
	    while(a[k]<=p){
	        l=k;
	        k++;  
                    if(k==12){
                    break;}
	    } 
             
                
	    int count=0;
	    while(a[l]>=1){
	        
	        count= count+(p/a[l]);
                    p=p%a[l];
	        l=l-1;
                    if(l<0){
                    break;}
                    
	    }
	    
	   System.out.println(count);
	 }		
	    
	
}

}

i need to convert the set of number into “k”, “mil”, “bil” for my php website (Soundcloud downloader) when i implement this code the website automatically turns into error 500. Expert advice needed

I found one of the best solution for it, ie: convert decimal to binary and count the number of ones in it. that’s the answer.

for eg:

decimal: 253

binary: 11111101

no of ones: 7 that’s the answer.

For No > 2048, you have to use rest logic.

Thnx.

1 Like

Hey i tried the code using DP but the solution is not being accepted ,
the code works for the sample test cases and any other test case that i can think of .
Please help
Here is the link to my code :
https://www.codechef.com/viewsolution/14492082

can any one please check my code .I don’t know what is wrong it passed all sample tests.
https://www.codechef.com/viewsolution/17515413

Hello, guys, I have a website for downloading music from SoundCloud.
http://soundcloudmp3downloader.com/

And I hope u could help me… how can I get the information about bitrate of SC tracks?
is it possible? any idea?

My solution is a bit different and is very short if you are using C++ and stl as your tool to solve the problem
It uses a greedy approach to find the nearest value just smaller than or equal to n from the menu list.

    while (n > 0) {
        long nearest = upper_bound(menuList.begin(), menuList.end(), n) - menuList.begin();
        n -= menuList[nearest - 1];
        ++count;
    }

Here menuList is an array of the powers of 2 up to the required item - [1,2,4,8…]

int price[12] = {1, 2, 4, 8, 16, 32,
64, 128, 256, 512, 1024, 2048};

int t, p, c;
cin >> t;

for (int k = 0; k < t; k++)
{
    c = 0;
    cin >> p;
    while(p != 0) 
    {
        for (int i = 11; i >= 0; i--)
        {
            if (p >= price[i])
            {
                p -= price[i];
                c++;
                break;
            }
        }
    }
    cout << c << endl;
}
1 Like

if someone interested in dynamic programming solution

#include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>



using namespace std;
  #define MAX 1000007
   vector<int>v(12);
   vector<int>dp(100002,-1);


 int recu(int p)
    {
      if(p==0)
       return 0;
       if(p<0)
       return MAX;
    
      if(dp[p]!=-1)
       return dp[p];
       int q=MAX;
       for(int i=0;i<12;i++)
       {
        q=min(q,recu(p-v[i])+1);
       }
      return dp[p]=q;
    }


  int main()
        {
        int t,p;
        cin>>t;
        
        dp[0]=0;
        dp[1]=1;
        for(int i=0;i<12;i++)
        {
          v[i]=(1<<i);
        }
        
        while(t--)
        { 
            int ans=0;
           cin>>p;
     
           ans=recu(p);
          cout<<ans<<"\n";
    
       }
    return 0;
    }

link text

I am not familiar with Python but it seems that you are using file for input. You should read from the standard input and output to the standard output.

1 Like

I actually am using standard input. It reads from an input file on my computer (for testing), but if that file doesn’t exist (when I submit) it uses raw_input() instead. I’ve tested this method on the codechef judge and it works for other problems, so I don’t understand why it gets runtime error now.

1 Like