AcWing 4982. 进制
原题链接
中等
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL a, b;
int cal(string s1, string s2) {
int len1 = s1.length(), len2 = s2.length();
int res = 0;
for(int i = len1 + 1; i <= len2 - 1; i ++)
res += (i - 1);
if(len1 != len2) {
string s3 = "", s4 = "";
for(int i = 1; i <= len1; i ++) s3 += '1';
for(int i = 1; i <= len2; i ++) s4 += '1';
for(int i = 1; i < len1; i ++) {
string tmp = s3;
tmp[i] = '0';
if(tmp >= s1) res ++;
}
for(int i = 1; i < len2; i ++) {
string tmp = s4;
tmp[i] = '0';
if(tmp <= s2) res ++;
}
} else {
string s3 = "";
for(int i = 1; i <= len1; i ++) s3 += '1';
for(int i = 1; i < len1; i ++) {
string tmp = s3;
tmp[i] = '0';
if(tmp >= s1 && tmp <= s2) res ++;
}
}
return res;
}
void yuri() {
LL x1 = a, x2 = b;
string s1 = "", s2 = "";
while(x1 != 0) {
char ch = (x1 % 2) + '0';
s1 += ch;
x1 /= 2;
}
reverse(s1.begin(), s1.end());
while(x2 != 0) {
char ch = (x2 % 2) + '0';
s2 += ch;
x2 /= 2;
}
reverse(s2.begin(), s2.end());
cout << cal(s1, s2) << '\n';
}
int main() {
cin >> a >> b;
yuri();
return 0;
}