Problem:
Score : 100 points

Problem Statement

How many multiples of d are there among the integers between L and R (inclusive)?

Constraints

All values in input are integers.

1 \leq L \leq R \leq 100

1 \leq d \leq 100

Input

Input is given from Standard Input in the following format:

L R d

Output

Print the number of multiples of d among the integers between L and R (inclusive).

Sample Input 1

5 10 2

Sample Output 1

3

Among the integers between 5 and 10, there are three multiples of 2: 6, 8, and 10.

Sample Input 2

6 20 7

Sample Output 2

2

Among the integers between 6 and 20, there are two multiples of 7: 7 and 14.

Sample Input 3

1 100 1

Sample Output 3

100

Solution in C (GCC 9.2.1) (judge verdict: Runtime Error)

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int popcount(int n);

void main(void){
    int X,N,pc=0;
    scanf("%d",&N);
    int by[N];
    int i = 0;
    char ch[N];
    scanf("%s",ch);

    //charで読み込んだ01の二進数をintにしてby[]に格納
    for(int i = 0;i < N;i++){
        if(ch[i] == '0')by[i] = 0;
        else {
            by[i] = 1;
            pc++;//1の時カウント
        }
        
    }
    //この時点で最初に与えられたN桁の二進数の1の数がpcに格納される
    int Fpc = pc;//初期の1の数をメモ

    //pow[n]に2^nの値を格納
    int pow[N];
    pow[N-1] = 1;
    for(int k = N-2;k >= 0;k--){
        pow[k] = pow[k+1]*2;
    }

    //与えられたN桁の二進数を10進数に変換する
    int n = 0;
    for(int j = 0;j < N;j++){
            n += by[j]*pow[j];
        }
    int Fn = n;//nの初期値をメモ
    
    //ここからf(Xi)を計算する処理
    for(int i = 0;i < N;i++){
        int count = 0;
        pc = Fpc;
        n = Fn;

        //i番目が0の時反転させて1にその分1の数が増え、nも増加するためn = n + 2^(N-i)
        if(by[i] == 0){
            pc++;
            n += pow[i];
        }
        //i番目が1の時反転させて0に1が減るため、n = n - 2^(N-i)
        else{
            pc--;
            n -= pow[i];
        }

        //nをpcで割った余りを新しいnとし、新しいnの2進表記の1の数を求める。
        while (n != 0){
            n = n%pc;
            count++;
            pc = popcount(n);
        }

        printf("%d\n",count);
          
    }
}

int popcount(int n){
    int B[200000];
    int i = 0,sum = 0;
    while(n != 0){
        B[i] = n%2;
        n = (n-B[i])/2;
        if(B[i] == 1)sum++;
        i++;
    }
    return sum;
}