Binary and 3

You are given <this> binary number B = b 1 b 2 b n B = b_1b_2\cdots b_n

S S is the set of all binary numbers of the form b i b i 1 b i 2 b 2 b 1 b_{i}b_{i-1}b_{i-2}\cdots b_2b_1 where 1 i n 1 \leq i \leq n

How many numbers in S S are divisible by 3 (i.e. 1 1 2 11_2 )?


Inspiration


The answer is 43987.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Rushikesh Jogdand
Feb 18, 2017

2 k 1 m o d 3 2^k \equiv 1 \mod 3 if k 0 m o d 2 k \equiv 0 \mod 2

2 k 2 m o d 3 2^k \equiv 2 \mod 3 if k 1 m o d 2 k \equiv 1 \mod 2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <stdio.h>
int main()
{
    char c;
    int count = 0, sum = 0, bit;
    char rem_two = 0;
    scanf("%c", &c);
    while(c == '1' || c == '0'){
        bit = c - 48;
        sum += (rem_two ? 2 : 1) * bit;
        sum %= 3;
        if(sum == 0) count += 1;
        rem_two = ~rem_two;
        scanf("%c", &c);
    }
    printf("%d", count);
}

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...