Sheldon Numbers


题目链接:https://vjudge.net/contest/176430#problem/H

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?
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 file contains several test cases, each of them as described below.
The input contains one line, with two space separated integer numbers, X and Y .
Constraints:
0 ≤ X ≤ Y ≤ 2^63

Output


For each test case, 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 .
Notes:
Explanation of output 1. All numbers between 1 and 10 are Sheldon Numbers.
Explanation of output 2. 73 is the only Sheldon number in this range.

Sample Input


1 10
70 75

Sample Output


10
1



题目大意:要求输入两个数x和y(0 ≤ X ≤ Y ≤ 2^63)在这两个数之间找出所有二进制可以表示成ABAB...AB或者是

ABAB...ABA的形式的数,A代表一串‘1’,B代表一串‘0’。可以没有B,但是必须要有A。

例如:11111111-----------A;

1100110011---------ABABA;

110000---------AB;

1111110111111------ABA;

#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#define ll long long
using namespace std;
set<ll> ans;
void init(){
	for(int n=1;n<64;n++){//每一小段A或B的长度;
		for(int m=0;m<64;m++){
			for(int len=1;len<=64;len++){
				if(!((len%(n+m)==0)||(len%(n+m)==n)))
					continue;
				ll cur=0; 
				bool flag=1;
				int i=0;
				while(1){
					if(i>=len)
						break;
					if(flag){
						flag=0;
						cur<<=n;
						i+=n;
						cur+=(1ll<<n)-1;
					}else{
						flag=1;
						cur<<=m;
						i+=m;
					}
				}
				ans.insert(cur);
			}
		}
	}
}

int main()
{
	init();
	int s=ans.size();
	ll l,r;
	while(scanf("%llu%llu",&l,&r)!=EOF){
		int Ans=0;
		set<ll>::iterator it;
		for(it=ans.begin();it!=ans.end();it++)
			if(*it>=l&&*it<=r)
				Ans++;
		printf("%d\n",Ans);
	}
	return 0;
}

参考文章:http://www.cnblogs.com/Sunshine-tcf/p/5769116.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值