Sheldon Numbers

题目链接:

http://acm.hust.edu.cn/vjudge/contest/127406#problem/H

Description

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]之间有多少个Sheldon Number:
其二进制表示满足 ABAB...AB 或 ABAB...ABA 的格式.
其中A为连续n(n>0)个1,B为连续m(n>0)个1.

题解:

一开始尝试打表,并在oeis上找到了序列(不过没公式).
后来换个方向从N和M的角度考虑:
对于一个满足条件的二进制串:如果N和M、长度这三个因素都固定了,那么这个串也就固定了.
所以符合条件的串的个数少于 64^3 个.
依次枚举N、M、长度这三个因素并构造符合条件的数,加入set判重.
实际上符合条件的数只有4810个.
注意:题目的右区间2^63超出了longlong范围,这里要用unsigned longlong. (%llu)
UVA上的题不要再用I64d了,已经被坑好几回了,老是忘.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <list>
#define LL unsigned long long
#define eps 1e-8
#define maxn 101000
#define mod 100000007
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;set<LL> ans;
void init() {for(int n=1; n<64; n++) {for(int m=0; m<64; m++) {for(int len=1; len<=64; len++) {if(!((len%(m+n)==0) || (len%(m+n)==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 {cur <<= m;flag = 1;i += m;}}ans.insert(cur);}}}
}int main(int argc, char const *argv[])
{//IN;init();int sz = 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", *it);}}printf("%d\n", Ans);}return 0;
}

转载于:https://www.cnblogs.com/Sunshine-tcf/p/5769116.html

UVALive 7279 Sheldon Numbers (暴力打表)相关推荐

  1. UVALive 7279 Sheldon Numbers

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  2. 7279 - Sheldon Numbers

    题目链接:点击打开链接 题意:A代表一到多个1,B代表一到多个0,组成二进制不长于63位的数字.形式为ABAB...A或ABABAB...AB: 分析:一个令我印象深刻的位运算题目,同时巩固了对set ...

  3. Sheldon Numbers 暴力枚举

    题意:求在区间内Sheldon数字的个数 题解: 枚举n  m,然后判断是否可以组成相应的位数 然后再判断是否这个数字是在这个区间内 #include<stdio.h> #include& ...

  4. Gym - 101128H - Sheldon Numbers

    Gym - 101128H - Sheldon Numbers 原命题链接 写在前面 这是一道在学校比赛的时候遇到的题目,当时就觉得可以敲,一开始是纯暴力枚举判断,结果正确但是严重超时,后来改成了暴力 ...

  5. [蓝桥杯][2016年第七届真题]冰雹数(暴力打表找规律)

    题目描述 任意给定一个正整数N, 如果是偶数,执行: N / 2 如果是奇数,执行: N * 3 + 1 生成的新的数字再执行同样的动作,循环往复. 通过观察发现,这个数字会一会儿上升到很高, 一会儿 ...

  6. 暴力打表之hdu 2089

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 有两种方法: 1.数位DP算法 2.暴力打表--真是个好法子!!! 接下来是注意点: 1.一般这 ...

  7. Sheldon Numbers (暴力枚举)

    一开始想到是暴力构造 但是题没读懂.还好队友读懂了.今天自己写了个 发现还挺好写的.改了几个点过了. #include <bits/stdc++.h> using namespace st ...

  8. Codeforces Gym 100418K Cards 暴力打表

    Cards Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  9. HDU1999不可摸数-暴力打表

    看到这约数和第一反应是约数和函数,然后仔细一看不是正经的约数和函数,就去推去了,然后推的有点小复杂.(数论函数那部分做多了) 然后观察也没有用到什么数论部分的特殊知识啊,难不成真的要暴力? 大概分析了 ...

最新文章

  1. mysql 0x80004005 unable to connect to any of the specified mysql hosts
  2. [BuildRelease]build number / id
  3. 在 OS X 中使用 OpenResty
  4. python的init函数里参数的作用
  5. LinkedHashMap 的理解以及借助其实现LRU
  6. Jupyter与PyCharm不可兼得?Jupytext就是你需要的!
  7. “软件宝宝”出生前,安全系列文章(一)
  8. linux查看log软件
  9. 二分法:木棒切割问题
  10. Unity3D之创建3D游戏场景
  11. 什么是servlet及其生命周期
  12. bootstrap table用法
  13. UVA 177 Paper Folding
  14. java毕业设计时装购物系统mybatis+源码+调试部署+系统+数据库+lw
  15. 船用炉灶的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  16. 网站调用服务器字体,网站调用字体库
  17. HDU-4515,小Q系列故事——世界上最遥远的距离(日期计算)
  18. 在线EXCEL编辑器-Luckysheet
  19. 学废了吗?2022年我的GTD工作流
  20. python | 短句自动生成SEO文章

热门文章

  1. 天融信 服务器映射,天融信(NAT)地址转换端口映射配置
  2. spring自定义定时任务- @Scheduled注解
  3. 同指数幂相减公式_同底指数加减运算法则
  4. 2021年12月中国A股生物制品行业上市企业市值排行榜:百济神州-U位居第二,康希诺-U股价最高(附月榜TOP43详单)
  5. 【立创开源】 立创EDA涂鸦智能 改造床头灯
  6. 正则表达式 密码 需包含字母数字特殊字符
  7. 云流量成为数据中心的王者 五年内全球超大规模IDC将不断出现
  8. PYTHON练习题---设有一头小母牛,从出生第四年起每年生一头小母牛
  9. HTML Input标签输入限制
  10. html中的colspan是什么意思