Submission #228738

#TimeUsernameProblemLanguageResultExecution timeMemory
228738Haunted_CppPalindrome-Free Numbers (BOI13_numbers)C++17
100 / 100
6 ms512 KiB
#include <iostream> #include <vector> #include <cstring> #include <cassert> #include <bitset> #pragma GCC optimize ("Ofast") #pragma GCC target("fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") #pragma GCC optimize("unroll-loops") using namespace std; long long dp [20][11][11][2]; vector<int> lim; const int OFF= 1; long long solve (int p, int last1, int last2, bool started, bool tight) { if (p < 0) return 1; if (tight && ~dp[p][last1 + OFF][last2 + OFF][started]) return dp[p][last1 + OFF][last2 + OFF][started]; long long res = 0; if (!started) { res += solve (p - 1, last1, last2, false, true); } for (int i = (started ? 0 : 1) ; i <= (tight ? 9 : lim[p]); i++) { int ant = last1 - OFF; int antant = last2 - OFF; int dig = i; if (dig != ant && dig != antant) { res += solve (p - 1, dig + OFF, ant + OFF, true, tight | (i < lim[p])); } } return (!tight ? res : dp[p][last1 + OFF][last2 + OFF][started] = res); } long long get_solve (long long n) { if (n < 0) return 0; lim.clear(); while (n) { lim.emplace_back(n % 10); n /= 10; } return solve ( (int) lim.size() - 1, -1 + OFF, -1 + OFF, false, false); } int main () { ios::sync_with_stdio(0); cin.tie(0); memset (dp, -1, sizeof(dp)); long long lo, hi; cin >> lo >> hi; cout << get_solve (hi) - get_solve (lo - 1) << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...