611B. New Year and Old Property【模拟位运算】

本文探讨了一种算法来计算特定区间内具有唯一二进制零的年份数量,通过位运算和数组模拟解决该问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. New Year and Old Property
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The year 2015 is almost over.

Limak is a little polar bear. He has recently learnt about the binary system. He noticed that the passing year has exactly one zero in its representation in the binary system — 201510 = 111110111112. Note that he doesn't care about the number of zeros in the decimal representation.

Limak chose some interval of years. He is going to count all years from this interval that have exactly one zero in the binary representation. Can you do it faster?

Assume that all positive integers are always written without leading zeros.

Input

The only line of the input contains two integers a and b (1 ≤ a ≤ b ≤ 1018) — the first year and the last year in Limak's interval respectively.

Output

Print one integer – the number of years Limak will count in his chosen interval.

Sample test(s)
input
5 10
output
2
input
2015 2015
output
1
input
100 105
output
0
input
72057594000000000 72057595000000000
output
26
Note

In the first sample Limak's interval contains numbers 510 = 1012610 = 1102710 = 1112810 = 10002910 = 10012 and1010 = 10102. Two of them (1012 and 1102) have the described property.


模拟一下位运算即可,当时本来还试了下真正的位运算,但总是这里不对那里不对,最后直接用数组模拟了。


#include <iostream>
#include <string>
#include <cstring>
using namespace std;

#define ULL unsigned long long
int bin[64+10];
ULL cal(int n)
{
	ULL ans=0;
	for(int i=1 ; i<=n ; ++i)
	{
		ans+=(ULL)bin[i]<<(i-1);
	}
	return ans;
}
int main(void)
{
	ULL a,b,cnt=0;
	cin>>a>>b;
	
	for(int i=0 ; i<=64 ; ++i)
		bin[i]=1;
	for(int i=2 ; i<=64 ; ++i)
	{
		for(int j=1 ; j<i ; ++j)
		{
			bin[j]=0;
			ULL ans=cal(i);
			if(ans>=a && ans<=b)
				cnt++;
			bin[j]=1;
		}
	}
	cout<<cnt<<"\n";
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值