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. 从IEEE754标准谈C语言浮点数据类型
  2. 差异分析定位Ring 3保护模块
  3. 在Linux 下配置PHP 支援GD
  4. SCCM2012 R2实战系列之九:OSD(中)-- 捕获镜像
  5. AngularJS的学习--$on、$emit和$broadcast的使用
  6. 机器学习降维算法一:PCA(主成分分析算法)
  7. ORACLE SQL*PLUS 命令大全
  8. 20135219洪韶武——信息安全系统设计基础第四周学习总结
  9. 关于vs编译器的一些认识
  10. 局域网内window10和Windows7共享只有USB接口打印机的方法——以sharp2048D为例子
  11. python下载kivy_下载、目录-『Python Kivy』Kivy and PyDev on Eclipse -by小雨
  12. matlab输出相反数,在MATLAB将等于某一数列相反数的数据都赋值为0
  13. OLED屏幕应用实验
  14. mpeg怎么转换成mp4?
  15. PgSQl 结合 Mybatis 插入 json,及查询,数据库使用 jsonb
  16. RIGHT-BICEP单元测试——“二柱子四则运算升级版”
  17. win10 自动同步时间脚本
  18. linux 内存清理 释放命令,linux 内存清理/释放命令总结
  19. robocup2D教程
  20. 【论文翻译】联合学习对齐和翻译的神经机器翻译

热门文章

  1. c语言输入f1到f11,我来告诉你:电脑键盘的F1至F12正确用处
  2. 计算机英语性考任务答案,2010秋英语(1)形考答案
  3. 目前月薪存一百万需要多久?
  4. 忆鲁迅《故乡》中的一句话
  5. 奇安信技术支持实习生面试
  6. Java—学生管理系统使用文件永久存储
  7. 0x00000709无法连接打印机怎么办?
  8. 118 Servlet_1 _Tomcat服务器
  9. 二叉树(从建树、遍历到存储)Java
  10. H.264是什么?H.265是什么?视频码率是什么?了解视频原理