Sheldon Numbers
According to Sheldon Cooper, the best number is 73. In his own words,
“The best number is 73. 73 is the 21st prime number. Its mirror, 37,
is the 12th, and its mirror, 21, is the product of multiplying 7 and 3.
In binary, 73 is a palindrome: 1001001, which backwards is 1001001.
Exactly the same.”
Prime numbers are boring stuff, and so are palindromes. On the other
hand, the binary representation of 73 is rather remarkable: it’s 1 one
followed by 2 zeroes, followed by 1 one, followed by 2 zeros, followed
by 1 one. This is an interesting pattern that we can generalize: N ones, followed by M
zeros, followed by N ones, followed by M zeros, etc, ending in either N ones or M zeroes.
For 73, N is 1, M is 2, and there are 5 runs of equal symbols. With N = 2, M = 1 and 4
runs, we would have the string 110110, which is the binary representation of 54.
Acknowledging Sheldon’s powerful insight, let us introduce the concept of a Sheldon number:
a positive integer whose binary representation matches the pattern ABABAB . . . ABA
or the pattern ABABAB . . . AB, where all the occurrences of A represent a string with
N occurrences of the bit 1 and where all the occurrences of B represent a string with M
occurrences of the bit 0, with N > 0 and M > 0. Furthermore, in the representation,
there must be at least one occurrence of the string A (but the number of occurrences of
the string B may be zero).
Many important numbers are Sheldon numbers: 1755, the year of the great Lisbon earthquake,
1984, of Orwellian fame, and 2015, the current year! Also, 21, which Sheldon
mentions, is a Sheldon number, and so is 42, the answer given by the Deep Thought
computer to the Great Question of Life, the Universe and Everything.
Clearly, there is an infinite number of Sheldon numbers, but are they more dense or less
dense than prime numbers?
Task
Your task is to write a program that, given two positive integers, computes the number of
Sheldon numbers that exist in the range defined by the given numbers.
Input
The input contains one line, with two space separated integer numbers, X and Y .
Constraints
0 ≤ X ≤ Y < 2
63
Universidade do Porto Computer Science Department 17
Problem H Problem H
Output
The output contains one line, with one number, representing the number of Sheldon
numbers that are greater or equal to X and less or equal to Y .
Sample Input 1
1 10
Sample Output 1
10
Output 1 Explanation
All numbers between 1 and 10 are Sheldon Numbers.
Sample Input 2
70 75
Sample Output 2
1
Output 2 Explanation
According to Sheldon Cooper, the best number is 73. In his own words,
“The best number is 73. 73 is the 21st prime number. Its mirror, 37,
is the 12th, and its mirror, 21, is the product of multiplying 7 and 3.
In binary, 73 is a palindrome: 1001001, which backwards is 1001001.
Exactly the same.”
Prime numbers are boring stuff, and so are palindromes. On the other
hand, the binary representation of 73 is rather remarkable: it’s 1 one
followed by 2 zeroes, followed by 1 one, followed by 2 zeros, followed
by 1 one. This is an interesting pattern that we can generalize: N ones, followed by M
zeros, followed by N ones, followed by M zeros, etc, ending in either N ones or M zeroes.
For 73, N is 1, M is 2, and there are 5 runs of equal symbols. With N = 2, M = 1 and 4
runs, we would have the string 110110, which is the binary representation of 54.
Acknowledging Sheldon’s powerful insight, let us introduce the concept of a Sheldon number:
a positive integer whose binary representation matches the pattern ABABAB . . . ABA
or the pattern ABABAB . . . AB, where all the occurrences of A represent a string with
N occurrences of the bit 1 and where all the occurrences of B represent a string with M
occurrences of the bit 0, with N > 0 and M > 0. Furthermore, in the representation,
there must be at least one occurrence of the string A (but the number of occurrences of
the string B may be zero).
Many important numbers are Sheldon numbers: 1755, the year of the great Lisbon earthquake,
1984, of Orwellian fame, and 2015, the current year! Also, 21, which Sheldon
mentions, is a Sheldon number, and so is 42, the answer given by the Deep Thought
computer to the Great Question of Life, the Universe and Everything.
Clearly, there is an infinite number of Sheldon numbers, but are they more dense or less
dense than prime numbers?
Task
Your task is to write a program that, given two positive integers, computes the number of
Sheldon numbers that exist in the range defined by the given numbers.
Input
The input contains one line, with two space separated integer numbers, X and Y .
Constraints
0 ≤ X ≤ Y < 2
63
Universidade do Porto Computer Science Department 17
Problem H Problem H
Output
The output contains one line, with one number, representing the number of Sheldon
numbers that are greater or equal to X and less or equal to Y .
Sample Input 1
1 10
Sample Output 1
10
Output 1 Explanation
All numbers between 1 and 10 are Sheldon Numbers.
Sample Input 2
70 75
Sample Output 2
1
Output 2 Explanation
73 is the only Sheldon number in this range.
题意:ABA,ABAB.......串.如:A (11) , B(00) , 则AB(1100) , ABA(110011)符合条件。
1100(二进制) = 12(十进制) 。输入X,Y(十进制),问在 区间[x,y]中有多少个符合条件的数。
解题思路:可以先直接暴力枚举出所有符合条件的数(不会很多,就几千个),再用二分法搜索得出答案。
补充知识:位运算 4 << 1 ⇔ 100 (4的二进制) 左移1位 ⇔ 1000
4 | 1 ⇔ 100 | 001 ⇔ 101
#include<bits/stdc++.h>
using namespace std;
long long s[1000000];
int main(){
long long r,l,num = 1,a,all = 0,p1,p2;
s[++all] = num;
for(int i = 0;i < 63;i++){//这里判断全是1的情况
num = num<<1|1;
s[++all] = num;
}
for(int i = 1;i <= 63;i++){//i:开头有连续的 i 个 1
for(int j = 1;j <= 63;j++){//j:开头连续 i 个 1 后有连续 j 个 0(最小的结构)
num = 1;
for(int k = 1;k < i;k++){
num = num<<1|1;
}
a = num;
if(i + j <= 63){//这个就相当于初始化,比如这是110
num = num<<j;
s[++all] = num;
}
if(i + j + i <= 63){//这就是11011
s[++all] = num<<i|a;
}
int len1 = i,len2 = j,len = i + j;
while(1){
if(len1 + len2 + len <= 63){//这就是110110
num = num<<len|num;
s[++all] = num;
}
else break;
if(len1 + len2 + len + i <= 63){//这就是11011011
s[++all] = num<<i|a;
}
len1 = len1 + i,len2 = len2 + j;
}
}
}
cin>>l>>r;
sort(s + 1,s + all + 1);
p1 = lower_bound(s + 1,s + all + 1,l) - (s);//找到第一个大于等于 l 的数组的下标
p2 = upper_bound(s + 1,s + all + 1,r) - (s);//找到第一个大于 r 的数组的下标
cout<<p2 - p1;
}