| constexpr int DIGIT = 20; | |
| constexpr int STATUS = 60'000; | |
| constexpr int M = 12; | |
| /** | |
| * @brief | |
| * 3 ** 10 (in Python) | |
| * or 3 ^ 10 (in some other languages) | |
| */ | |
| namespace sol | |
| { | |
| int digit[DIGIT]; | |
| unsigned long long f[DIGIT][STATUS]; | |
| unsigned long long m[M]; | |
| int digit_cnt; | |
| inline int upd(int x, int p) | |
| { | |
| register int res = 0; | |
| for (register int i = 0; i < 10; i++) | |
| { | |
| register int _cac = x % 3; | |
| x /= 3; | |
| res += (i == p ? 1 + (_cac == 1) : _cac) * m[i]; | |
| } | |
| return res; | |
| } | |
| inline bool check(int x) | |
| { | |
| register int p = 0; | |
| while (x) | |
| { | |
| if ((x % 3 == 2 && p % 2 == 0) || (x % 3 == 1 && p % 2 == 1)) | |
| { | |
| return 0; | |
| } | |
| x /= 3; | |
| p++; | |
| } | |
| return 1; | |
| } | |
| unsigned long long dfs(int pos, int status, bool zero, bool limit) | |
| { | |
| if (pos == -1) | |
| { | |
| return check(status); | |
| } | |
| if (!limit && f[pos][status] != -1) | |
| { | |
| return f[pos][status]; | |
| } | |
| int up = limit ? digit[pos] : 9; | |
| unsigned long long res = 0; | |
| for (int i = 0; i <= up; i++) | |
| { | |
| register bool _cac_zero_i = (zero && i == 0), _cac_limit_i = (limit && i == up); | |
| res += dfs(pos - 1, _cac_zero_i ? 0 : upd(status, i), _cac_zero_i, _cac_limit_i); | |
| } | |
| if (!limit) | |
| { | |
| f[pos][status] = res; | |
| } | |
| return res; | |
| } | |
| inline void decomp(unsigned long long &x) | |
| { | |
| digit_cnt = 0; | |
| while (x) | |
| { | |
| digit[digit_cnt++] = x % 10; | |
| x /= 10; | |
| } | |
| } | |
| inline unsigned long long solve(unsigned long long x) | |
| { | |
| decomp(x); | |
| return dfs(digit_cnt - 1, 0, 1, 1); | |
| } | |
| } // namespace sol | |
| namespace ma | |
| { | |
| using namespace sol; | |
| unsigned long long L, R; | |
| inline void init() | |
| { | |
| m[0] = 1; | |
| for (register int i = 1; i < M; i++) | |
| { | |
| m[i] = m[i - 1] * 3; | |
| } | |
| memset(f, -1, sizeof(f)); | |
| } | |
| inline void in() | |
| { | |
| init(); | |
| int T; | |
| io::read(T); | |
| for (register int _ = 0; _ < T; _++) | |
| { | |
| io::read(L, R); | |
| io::writeln(solve(R) - solve(L - 1)); | |
| } | |
| } | |
| } // namespace ma | |
| int main() | |
| { | |
| ma::in(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment