Gym - 101128H:Sheldon Numbers
题意
给你两个整数X和Y
问你在区间[X,Y]中,有多少数字的二进制满足ABAB或者A这种形式。A是某个数量的1,B是某个数量的0。
分析
因为数据规模很大,直接枚举x和y之间的数字然后判断会超时。所以直接构造符合的二进制串然后判断是不是在区间[X,Y]内就好。因为二进制串的长度最多只有63,所以随便枚举就行。但是写起来确实麻烦,容易出错的地方太多。
下面的代码是在场上两个队友写的,场上的代码嘛~可能有点乱~(好吧是我懒得再写一遍了
1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <algorithm> 5 #include <set> 6 #include <map> 7 #include <vector> 8 #include <queue> 9 #include <stack> 10 #include <iostream> 11 #include <cmath> 12 13 using namespace std; 14 typedef long long LL; 15 typedef unsigned long long ull; 16 const int INF=2147483000; 17 const int maxn=200000+10; 18 19 20 ull l, r; 21 string up, down; 22 ull ans = 0; 23 24 25 int lup, ldown; 26 27 int check(string &tmp) 28 { 29 if(tmp == "") return 0; 30 int len = tmp.size(); 31 32 if (len > lup && len < ldown) return 1; 33 if (len == lup && len != ldown && tmp >= up) return 1; 34 if (len == ldown && len != lup && tmp <= down) return 1; 35 if (len == lup && len == ldown && tmp >= up && tmp <= down) return 1; 36 return 0; 37 } 38 39 int main(){ 40 41 cin >> l >> r; 42 43 up = down = ""; 44 while(l) 45 { 46 up = (char)(l % 2 + '0') + up; 47 //cout << up << endl; 48 l >>= 1; 49 } 50 51 while(r) 52 { 53 down = (char)(r % 2 + '0') + down; 54 r >>= 1; 55 } 56 57 lup = up.size(), ldown = down.size(); 58 59 // string t; 60 // cin >> t; 61 // cout << check(t) << endl; 62 63 64 for (int len = lup; len <= ldown; len++) 65 { 66 //cout << len << endl; 67 string tmp = ""; 68 for (int i = 1; i <= len; i++) 69 { 70 string one = ""; 71 for (int tim = 1; tim <= i; tim++) 72 one += '1'; 73 74 for (int j = 1; j <= len; j++) 75 { 76 if(i + j > len) continue; 77 78 string zero = ""; 79 for (int tim = 1; tim <= j; tim++) 80 zero += '0'; 81 82 if (len%(i+j) != 0 && len%(i+j) != i) continue; 83 84 tmp = ""; 85 int maxtim = len/(i+j); 86 87 for (int tim = 1; tim <= maxtim; tim++) 88 tmp += one+zero; 89 90 if (len % (i+j) != 0) 91 tmp += one; 92 93 if (check(tmp)) 94 { 95 ans++; 96 //cout << tmp << endl; 97 } 98 99 //cout << tmp << endl; 100 } 101 } 102 103 tmp = ""; 104 for (int tim = 1; tim <= len; tim++) 105 tmp += '1'; 106 if (check(tmp)) 107 { 108 ans++; 109 //cout << tmp << endl; 110 } 111 } 112 113 cout << ans << endl; 114 return 0; 115 116 }