UVALive 7279 Sheldon Numbers (暴力打表)
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了,已经被坑好几回了,老是忘.
##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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 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;
}
UVALive 7279 Sheldon Numbers (暴力打表)的更多相关文章
- codeforces 9 div2 C.Hexadecimal's Numbers 暴力打表
C. Hexadecimal's Numbers time limit per test 1 second memory limit per test 64 megabytes input stand ...
- HNUSTOJ-1565 Vampire Numbers(暴力打表)
1565: Vampire Numbers 时间限制: 3 Sec 内存限制: 128 MB提交: 20 解决: 9[提交][状态][讨论版] 题目描述 The number 1827 is an ...
- XTU OJ 1210 Happy Number (暴力+打表)
Problem Description Recently, Mr. Xie learn the concept of happy number. A happy number is a number ...
- HDU 1216 Assistance Required(暴力打表)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1216 Assistance Required Time Limit: 2000/1000 MS (Ja ...
- UVA - 13022 Sheldon Numbers(位运算)
UVA - 13022 Sheldon Numbers 二进制形式满足ABA,ABAB数的个数(A为一定长度的1,B为一定长度的0). 其实就是寻找在二进制中满足所有的1串具有相同的长度,所有的0串也 ...
- ACM/ICPC 之 暴力打表(求解欧拉回路)-编码(POJ1780)
///找到一个数字序列包含所有n位数(连续)一次且仅一次 ///暴力打表 ///Time:141Ms Memory:2260K #include<iostream> #include< ...
- 【ZOJ】3785 What day is that day? ——浅谈KMP在ACM竞赛中的暴力打表找规律中的应用
转载请声明出处:http://www.cnblogs.com/kevince/p/3887827.html ——By Kevince 首先声明一下,这里的规律指的是循环,即找到最小循环周期. 这 ...
- HDU 1012 u Calculate e【暴力打表,水】
u Calculate e Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- Codeforces 914 C 数位DP+暴力打表+思维
题意 给出一个二进制数\(n\),每次操作可以将一个整数\(x\)简化为\(x\)的二进制表示中\(1\)的个数,如果一个数简化为\(1\)所需的最小次数为\(k\),将这个数叫做特殊的数, 问从\( ...
随机推荐
- 24-语言入门-24-cigarettes
题目地址: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=94 描述Tom has many cigarettes. We hypoth ...
- BZOJ 1004 Cards(Burnside引理+DP)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1004 题意:三种颜色的扑克牌各有Sr,Sb,Sg张.给出m种置换.两种染色方案在某种置换 ...
- 让Eclipse和NetBeans共享同一个项目
有的时候,我们会下载一些源代码来学习研究,但是下载下来的工程文件是eclipse的或者是NetBeans的.如果手头上没有eclipse或者没有 NetBeans,或只有其中一个怎么办?又或者,你习惯 ...
- LA 3029 - City Game (简单扫描线)
题目链接 题意:给一个m*n的矩阵, 其中一些格子是空地(F), 其他是障碍(R).找一个全部由F 组成的面积最大的子矩阵, 输出其面积乘以3的结果. 思路:如果用枚举的方法,时间复杂度是O(m^2 ...
- Android 怎样使用API
本文针对Android开发如何使用API文档进行一些经验分享. 1.为什么需要掌握API的使用. 也许你需要完成一个功能时很多时候你在网上google一番,因为很可能找到有用的代码片段,甚至不用关心具 ...
- Java知识点:内部类
内部类class文件命名规则 普通内部类.静态内部类:<Outer>\$<Inner>.class,其中<Outer>为外部类类名,<Inner>为内部 ...
- AngularJS 拦截器和应用例子(转)
$httpAngularJS 的 $http 服务允许我们通过发送 HTTP 请求方式与后台进行通信.在某些情况下,我们希望可以俘获所有的请求,并且在将其发送到服务端之前进行操作.还有一些情况是,我们 ...
- poj 1742 Coins
// v给出N种硬币和个数,问可以取到1->M中的多少个值.// 背包 完全背包 或多 重背包(二进制优化)都可以做// #include <iostream> #include & ...
- 【转】Github轻松上手1-Git的工作原理与设置
转自:http://blog.sina.com.cn/s/blog_4b55f6860100zzgp.html 作为一个程序猿,如果没有接触过stack overflow和Github,就如同在江湖中 ...
- WdatePicker的一些用法
在选择日期之后,再执行自己的另一个函数 onpicked: function () { LoadData(); } <input type="text" class=& ...