Sheldon Numbers GYM

题意:定义Sheldon Number为其二进制数是ABA……ABA型的或者ABAB……AB的,其中A全为1,B全为0(A>0, B>0),问[m, n]中有多少个Sheldon Number.

思路:只需要存储好所有A与B的情况,并枚举所有的ABA……ABA型的或者ABAB……AB的情况。

# include <bits/stdc++.h>
using namespace std; # define vi vector < int >
# define pb push_back
# define LL long long LL n, m;
string S[][];
set<LL > t; LL cal(string s){
LL sum = ;
for(int i = ; i < s.size(); i++){
sum *= ;
sum += s[i]-'';
}
return sum;
} LL solve(int a, int b){
string s;
LL k;
s += S[][a];
while(s.size()<=){
s += S[][b];
if(s.size() <=) {
k = cal(s);
if(k >= n && k <= m) {
t.insert(k);
// cout<<k<<" "<<s<<endl;
}
}
s += S[][a];
if(s.size() <=) {
k = cal(s);
if(k >= n && k <= m) {
t.insert(k);
// cout<<k<<" "<<s<<endl;
}
}
}
} void init(){
t.clear();
for(int i = ; i < ; i++){
for(int j = ; j < i; j++){
S[][i] += "";
S[][i] += "";
}
// cout<<S[0][i]<<" "<<S[1][i]<<endl;
} for(int i = ; i < ; i++){
for(int j = ; j < ; j++){
solve(i, j);
}
}
cout<<t.size();
} int main(void)
{
// freopen("input.txt", "r", stdin);
cin>>n>>m;
init();
return ;
}
05-24 07:36