DC [25/05/2021]

Example Input/Output 1:

Input:

52 2


Output:

3


Explanation:

The binary representation of 52 is B = 110100.

All prefixes in B and the corresponding decimal equivalents are given below.

110100 -> 52

11010 -> 26

1101 -> 13

110 -> 6

11 -> 3

1 -> 1

Here 52, 26 and 6 are divisible by 2. So the count 3 is printed as the output.


Example Input/Output 2:

Input:

147 3


Output:

4


Example Input/Output 3:

Input:

98 6


Output:

3

Program in c:

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

int main()

{

    int n,x;

    scanf("%d %d",&n,&x);

    int binary[64],l=0;

    while(n!=0)

    {

        binary[l++]=n%2;

        n/=2;

    }

    int count=0;

    for(int i=0;i<l;i++)

    {

        int currdecimal=0,power=0;

        for(int j=i;j<l;j++,power++)

        {

            if(binary[j]==1)

                currdecimal+=pow(2,power);

        }

        if(currdecimal%x==0)

            count++;

    }

    printf("%d",count);

    return 0;

} 

Comments

Popular posts from this blog

alphabet matrix minimum sum path

C--Starter-part007---skillrack