Submission #95270

#TimeUsernameProblemLanguageResultExecution timeMemory
95270rkocharyanPalindrome-Free Numbers (BOI13_numbers)C++14
81.25 / 100
18 ms444 KiB
#include <bits/stdc++.h> using namespace std; const int N = 20; int n; long long dp[N][2]; string s; long long calc(int pos, int f, int c1, int c2, int c3) { if((c2 == c3 && c2 != -1 && c3 != -1) || (c1 == c3 && c1 != -1 && c3 != -1)) return 0; if(pos == n) return 1; auto &res = dp[pos][f]; if(res != -1) { return res; } res = 0; int lmt = f ? 9 : s[pos] - '0'; for(int i = 0; i <= lmt; i++) { int nf = f; if(i < lmt) nf = 1; res += calc(pos + 1, nf, c2, c3, i); } return res; } long long solve(string &x) { s = x; n = (int) x.size(); memset(dp, -1, sizeof dp); return calc(0, 0, -1, -1, -1); } bool check(long long x) { string s = to_string(x); int m = (int) s.size(); for(int i = 0; i < m - 1; i++) { if(s[i] == s[i + 1]) { return 0; } } for(int i = 0; i < m - 2; i++) { if(s[i] == s[i + 2]) { return 0; } } return 1; } long long brute(long long l, long long r) { int ans = 0; for(long long i = l + 1; i <= r; i++) { ans += check(i); } return ans; } int main() { ios::sync_with_stdio(0); cin.tie(0); long long l, r; cin >> l >> r; l--; string a, b; a = to_string(l); b = to_string(r); if(l == -1) { cout << solve(b) + 1 << '\n'; return 0; } if(r - l <= 1e6) { cout << brute(l, r) << '\n'; } else { cout << solve(b) - solve(a) << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...