Gym 101128H想法题
来源:互联网 发布:高达钢材软件 编辑:程序博客网 时间:2024/06/03 14:19
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.
【题意】
给定x,y问[x,y]间有多少数满足条件。
条件:二进制下形式为ABABAB……A,或ABABAB……AB,其中A为任意个数的1,B为任意个数的0;
【解法】
求[x,y]之间的个数可以转化成求[1,y]-[1,x-1],下面讨论求[1,x]的满足题意的数的个数。
枚举A中1的个数(最多63个)和B中0的个数(最多62个),用sum记录答案。起始为AB,每次加上一个A,sum++,再加一个B,sum++,如此循环直到当前的数超过x;
怎么解决溢出问题呢?
我是声明一个变量len,表示当前ABABABAB……串的长度,大于等于64就break。
#include<cstdio>typedef __int64 ll;int n,m;ll x,y;ll calc(ll x){ll len=0;ll num1=0,num2=0,sum=0; for(int n=1;n<=63;n++){//Anum1=(num1<<1)+1;//B if(num1>x)break;sum++;//全都是1的情况 for(int m=1;m<=62;m++){//造零的 if(m==61){int x=1;}len=n;num2=num1;num2=num2<<m;int f=2;len+=m;if(len>=64)break;//超出__int64范围了就得停了哦,不然上溢了会有问题。 while(num2<=x){sum++;if(f==2){//当前B后面再加A len+=n;f=1;num2=(num2<<n)+num1;//比如1100 - >110000+11==110011,就把A加上啦 }else if(f==1){//当前A后面加B的过程 len+=m;f=2;num2=num2<<m;//加上m个0 ,也就是加个B~\(≧▽≦)/~啦啦啦 ,比如110011->11001100; }if(len>=64)break;}}}return sum; }int main(){scanf("%I64d%I64d",&x,&y);printf("%I64d\n",calc(y)-calc(x-1));return 0;}
阅读全文
1 0
- Gym 101128H想法题
- Gym 101128C想法题
- gym 101194 china final Problem H. Great Cells(数学,想法题,好题)
- Gym-101177B 递归想法题
- GYM 101128 H.Sheldon Numbers(枚举)
- Gym 100917H Hierarchy 【STL好题】
- Codeforces Gym 100203D Different vectors 想法题 + Hash
- Codeforces gym 101350F 想法
- Codeforces gym 101149 G 想法
- Codeforces gym 101149 K 想法
- Codeforces gym 101102 K 想法
- GYM 100488 Two Pirates(想法)
- GYM 100523H
- codeforces GYM 100792H
- Gym 100712H Bridges
- Gym 100947H-Phobia
- Gym - 100735H
- Gym-101490H
- FICO初始基本操作流程
- qmake手册——平台笔记
- 完全函数依赖和部分函数依赖
- XListView 展示刷新与加载
- 复制字符串小程序笔记
- Gym 101128H想法题
- 分治策略 | 最大子数组问题
- 自定义Dialog, Toast提示
- 2729: [HNOI2012]排队
- 欢迎使用CSDN-markdown编辑器
- 机器学习:在Android中集成TensorFlow (深度学习,AI,人工智能,DL,ML,神经网络)
- MVC,MVP 和 MVVM
- UVA 10721 Bar Codes(容斥原理)
- Nginx配置文件详解