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[3][66]; set<LL > t; LL cal(string s){ LL sum = 0; for(int i = 0; i < s.size(); i++){ sum *= 2; sum += s[i]-'0'; } return sum; } LL solve(int a, int b){ string s; LL k; s += S[1][a]; while(s.size()<=63){ s += S[0][b]; if(s.size() <=63) { k = cal(s); if(k >= n && k <= m) { t.insert(k); // cout<<k<<" "<<s<<endl; } } s += S[1][a]; if(s.size() <=63) { k = cal(s); if(k >= n && k <= m) { t.insert(k); // cout<<k<<" "<<s<<endl; } } } } void init(){ t.clear(); for(int i = 1; i < 65; i++){ for(int j = 0; j < i; j++){ S[0][i] += "0"; S[1][i] += "1"; } // cout<<S[0][i]<<" "<<S[1][i]<<endl; } for(int i = 1; i < 63; i++){ for(int j = 0; j < 63; j++){ solve(i, j); } } cout<<t.size(); } int main(void) { // freopen("input.txt", "r", stdin); cin>>n>>m; init(); return 0; }