Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3338120
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:20:12+00:00 2026-05-18T00:20:12+00:00

So I just got back for the ACM Programing competition and did pretty well

  • 0

So I just got back for the ACM Programing competition and did pretty well but there was one problem that not one team got.

The Problem.

Start with an integer N0 which is greater than 0. Let N1 be the number of ones in the binary representation of N0. So, if N0 = 27, N1 = 4. For all i > 0, let Ni be the number of ones in the binary representation of Ni-1. This sequence will always converge to one. For any starting number, N0, let K be the minimum value of i >= 0 for which N1 = 1. For example, if N0 = 31, then N1 = 5, N2 = 2, N3 = 1, so K = 3.

Given a range of consecutive numbers and a value of X how many numbers in the range have a K value equal to X?

Input
There will be several test cases in the input. Each test case will consist of three integers on a single line:
LO HI X
Where LO and HI (1 <= LO <= HI <= 10^18) are the lower and upper limits of a range of integers, and X (0 <= X <= 10) is the target value for K. The input will end with a line of three 0s.

Output
For each test case output a single integer, representing the number of integers in the range from LO to HI (inclusive) which have a K value equal to X in the input. Print each Integer on its own line with no spaces. Do not print any blank lines between answers.

Sample Input

31 31 3
31 31 1
27 31 1
27 31 2
1023 1025 1
1023 1025 2
0 0 0

Sample Output

1
0
0
3
1
1

If you guys want I can include our answer or our problem, because finding for a small range is easy but I will give you a hint first your program needs to run in seconds not minutes. We had a successful solution but not an efficient algorithm to use a range similar to

48238 10^18 9

Anyway good luck and if the community likes these we had some more we could not solve that could be some good brain teasers for you guys. The competition allows you to use Python, C++, or Java—all three are acceptable in an answer.


So as a hint my coach said to think of how binary numbers count rather than checking every bit. I think that gets us a lot closer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T00:20:12+00:00Added an answer on May 18, 2026 at 12:20 am

    I think a key is first understanding the pattern of K values and how rapidly it grows. Basically, you have:

    K(1) = 0
    K(X) = K(bitcount(X))+1 for X > 1
    

    So finding the smallest X values for a given K we see

    K(1) = 0
    K(2) = 1
    K(3) = 2
    K(7) = 3
    K(127) = 4
    K(170141183460469231731687303715884105727) = 5
    

    So for an example like 48238 10^18 9 the answer is trivially 0. K=0 only for 1, and K=1 only for powers of 2, so in the range of interest, we’ll pretty much only see K values of 2, 3 or 4, and never see K >= 5

    edit

    Ok, so we’re looking for an algorithm to count the number of values with K=2,3,4 in a range of value LO..HI without iterating over the entire range. So the first step is to find the number of values in the range with bitcount(x)==i for i = 1..59 (since we only care about values up to 10^18 and 10^18 < 2^60). So break down the range lo..hi into subranges that are a power of 2 size and differ only in their lower n bits — a range of the form x*(2^n)..(x+1)*(2^n)-1. We can break down the arbitray lo..hi range into such subranges easily. For each such subrange there will be choose(n, i) values with i+bitcount(x) set bits.
    So we just add all the subranges together to get a vector of counts for 1..59, which we then iterate over, adding together those elements with the same K value to get our answer.

    edit (fixed again to be be C89 compatible and work for lo=1/k=0)

    Here’s a C program to do what I previously described:

    #include <stdio.h>
    #include <string.h>
    #include <assert.h>
    
    int bitcount(long long x) {
        int rv = 0;
        while(x) { rv++; x &= x-1; }
        return rv; }
    
    long long choose(long long m, long long n) {
        long long rv = 1;
        int i;
        for (i = 0; i < n; i++) {
            rv *= m-i;
            rv /= i+1; }
        return rv; }
    
    void bitcounts_p2range(long long *counts, long long base, int l2range) {
        int i;
        assert((base & ((1LL << l2range) - 1)) == 0);
        counts += bitcount(base);
        for (i = 0; i <= l2range; i++)
            counts[i] += choose(l2range, i); }
    
    void bitcounts_range(long long *counts, long long lo, long long hi) {
        int l2range = 0;
        while (lo + (1LL << l2range) - 1 <= hi) {
            if (lo & (1LL << l2range)) {
                bitcounts_p2range(counts, lo, l2range);
                lo += 1LL << l2range; }
            l2range++; }
        while (l2range >= 0) {
            if (lo + (1LL << l2range) - 1 <= hi) {
                bitcounts_p2range(counts, lo, l2range);
                lo += 1LL << l2range; }
            l2range--; }
        assert(lo == hi+1); }
    
    int K(int x) {
        int rv = 0;
        while(x > 1) {
            x = bitcount(x);
            rv++; }
        return rv; }
    
    int main() {
        long long counts[64];
        long long lo, hi, total;
        int i, k;
        while (scanf("%lld%lld%d", &lo, &hi, &k) == 3) {
            if (lo < 1 || lo > hi || k < 0) break;
            if (lo == 0 || hi == 0 || k == 0) break;
            total = 0;
            if (lo == 1) {
                lo++;
                if (k == 0) total++; }
            memset(counts, 0, sizeof(counts));
            bitcounts_range(counts, lo, hi);
            for (i = 1; i < 64; i++)
                if (K(i)+1 == k)
                    total += counts[i];
            printf("%lld\n", total); }
        return 0; }
    

    which runs just fine for values up to 2^63-1 (LONGLONG_MAX).
    For 48238 1000000000000000000 3 it gives 513162479025364957, which certainly seems plausible

    edit

    giving the inputs of

    48238 1000000000000000000 1
    48238 1000000000000000000 2
    48238 1000000000000000000 3
    48238 1000000000000000000 4
    

    gives outputs of

    44
    87878254941659920
    513162479025364957
    398959266032926842
    

    Those add up to 999999999999951763 which is correct. The value for k=1 is correct (there are 44 powers of two in that range 2^16 up to 2^59). So while I’m not sure the other 3 values are correct, they’re certainly plausible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.