루미 알고리즘

#1655실버Binary Palindromes

PO 2026 학교 예선코딩출제 Harry Zhang영어 원문이 문제가 어렵다면 — 배우는 순서 보기

영어 원문입니다. 원문의 수식·표를 글자로 옮겨 모양이 다를 수 있습니다. 정확한 내용은 원문 페이지에서 확인하세요. 원문 페이지 열기 (새 창)

Problem

Mikaela studies interesting positive integers, and now wants to investigate numbers that are binary palindromes. To formally describe this, we define the following functions:

bin(n) = the binary representation of the integer n without leading zeros, reverse(s) = the string s written backwards.

An integer n is thus a binary palindrome if

bin(n) = reverse(bin(n)).

Note that a palindrome is a string that reads the same forwards and backwards.

Mikaela is particularly interested in binary palindromes that contain exactly k ones in their binary representation, and now wants to know how many such numbers exist in the interval [l, r].

Given three integers l, r, and k, determine how many integers x satisfy:

  1. The number x is between l and r, i.e. l ≤ x ≤ r,
  2. The number x is a binary palindrome,
  3. The binary representation of x contains exactly k ones.

Input

The input consists of a single line containing three integers l, r, k (1 ≤ l ≤ r ≤ 10^18, 1 ≤ k < 60).

Output

Print an integer: the number of integers in the interval [l, r] that are binary palindromes and have exactly k ones in their binary representation.

Scoring

Your solution will be tested on a set of test groups. To earn points for a group, you must pass all test cases in that group.

GroupPointsConstraints
120l,r ≤ 10^9
220k ≤ 4
360No additional constraints.

Explanation of Samples

In sample 1, we seek numbers in the interval [7, 10] that are both binary palindromes and have exactly two ones in their binary representation. There are only 4 numbers in the interval, and their binary representations are:

7 = 111_2 (palindrome, three ones) 8 = 1000_2 (not a palindrome) 9 = 1001_2 (palindrome, two ones) 10 = 1010_2 (not a palindrome)

The only number satisfying both conditions is 9, so the answer is 1.

In sample 2, we seek numbers in the interval [1, 6] that are both binary palindromes and have exactly two ones in their binary representation. There are only 6 numbers in the interval:

1 = 1_2 (palindrome, a single one) 2 = 10_2 (not a palindrome) 3 = 11_2 (palindrome, two ones) 4 = 100_2 (not a palindrome) 5 = 101_2 (palindrome, two ones) 6 = 110_2 (not a palindrome)

The numbers satisfying both conditions are 3 and 5, so the answer is 2.

예제

예제 1

입력
7 10 2
출력
1

예제 2

입력
1 6 2
출력
2

예제 3

입력
999 99999 6
출력
94

출처 표기

출처: Programmeringsolympiaden 2026년 학교 예선 · 원문 페이지 · 이용 조건 CC BY-SA · LaTeX 원문을 화면용 글로 옮겼습니다(수식·표의 모양이 다를 수 있습니다). 내용은 고치지 않았습니다.

크리에이티브 커먼즈 저작자표시-동일조건변경허락.

문제 출처: 한국정보올림피아드(KOI) (CC BY-NC-SA 4.0) · International Olympiad in Informatics (IOI) (CC BY) · Centre for Education in Mathematics and Computing (CEMC), University of Waterloo (CC BY-NC 4.0) · Programmeringsolympiaden (CC BY-SA) · CSES Problem Set (Antti Laaksonen) (CC BY-NC-SA 4.0). 루미 알고리즘은 위 단체들과 관계없는 별도의 서비스이며, 자료는 원문 그대로 싣습니다. 문제마다 출처와 이용 조건을 따로 표기합니다.