Run ID Author Problem Lang Verdict Time Memory Code Length Submit Time
318183 test1478963 守序数 T4 C++ Wrong Answer 5 MS 712 KB 1306 2026-04-20 15:28:25

Tests(2/10):


#include <iostream> #include <cstring> #include <cmath> using namespace std; typedef long long ll; int d[20], tot; ll dp[20][11][2]; ll dfs(int pos, int pre, int lim) { if (!pos) return 1; if (dp[pos][pre][lim] != -1) return dp[pos][pre][lim]; int up = lim ? d[pos] : 9; ll res = 0; for (int i = 0; i <= up; ++i) { if (pre == 10) { if (i == 0) res += dfs(pos - 1, 10, lim && (i == up)); else res += dfs(pos - 1, i, lim && (i == up)); } else { if (abs(i - pre) <= 2) res += dfs(pos - 1, i, lim && (i == up)); } } return dp[pos][pre][lim] = res; } ll calc(ll x) { tot = 0; while (x) { d[++tot] = x % 10; x /= 10; } memset(dp, -1, sizeof(dp)); return dfs(tot, 10, 1) - 1; } ll find(int n) { ll l = 1, r = 1e18, mid, ans; while (l <= r) { mid = l + r >> 1; if (calc(mid) >= n) { ans = mid; r = mid - 1; } else l = mid + 1; } return ans; } int main() { int n; cin >> n; cout << find(n) << endl; return 0; }


Judgement Protocol: