Submission #219583

#TimeUsernameProblemLanguageResultExecution timeMemory
219583sochoPalindrome-Free Numbers (BOI13_numbers)C++14
100 / 100
12 ms1024 KiB
#include "bits/stdc++.h" using namespace std; // #define endl '\n' // #define double long double #define int long long // int MOD = 1000 * 1000 * 1000 + 7; // int MOD = 998244353; // const int MXN = 100005; const int MXN = 20; int digits[MXN]; void load(int a) { for (int i=19; i>=0; i--) { digits[i] = a % 10; a /= 10; } } int dp[MXN][2][MXN][10][10]; void dpclear() { for (int i=0; i<MXN; i++) { for (int j=0; j<2; j++) { for (int k=0; k<MXN; k++) { for (int a=0; a<10; a++) { for (int b=0; b<10; b++) { dp[i][j][k][a][b] = -1; } } } } } } int solve(int at, bool atmin, int placed, int last1, int last2) { if (at == MXN) return 1; // finished // cout << at << ' ' << atmin << ' ' << placed << ' ' << last1 << ' ' << last2 << endl; if (dp[at][atmin][placed][last1][last2] != -1) return dp[at][atmin][placed][last1][last2]; int ans = 0; for (int i=0; i<=9; i++) { if (atmin && i > digits[at]) { // violates range continue; } else { bool mncond = atmin && (i == digits[at]); if (placed == 0) { // first digit, no restriction // cout << "tried placing: " << i << endl; if (i == 0) ans += solve(at+1, mncond, 0, i, 0); else ans += solve(at+1, mncond, placed+1, i, 0); } else if (placed == 1) { // second digit, only first restrict if (i != last1) { // cout << "tried placing: " << i << endl; ans += solve(at+1, mncond, placed+1, i, last1); } } else { // any other digit, all restrictions if (i != last1) { if (i != last2) { // cout << "tried placing: " << i << endl; ans += solve(at+1, mncond, placed+1, i, last1); } } } } } return dp[at][atmin][placed][last1][last2] = ans; } bool can(int a) { if (a <= 10) return true; string s = ""; while (a > 0) { int x = a % 10; a /= 10; s += ((char) '0' + x); } for (int i=0; i<s.size()-1; i++) { if (s[i] == s[i+1]) return false; } for (int i=0; i<s.size()-2; i++) { if (s[i] == s[i+2]) return false; } return true; } signed main() { int a, b; cin >> a >> b; load(b); dpclear(); int upto = solve(0, 1, 0, 0, 0); load(a); dpclear(); int unto = solve(0, 1, 0, 0, 0); cout << upto - unto + can(a) << endl; }

Compilation message (stderr)

numbers.cpp: In function 'bool can(long long int)':
numbers.cpp:85:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i=0; i<s.size()-1; i++) {
                ~^~~~~~~~~~~
numbers.cpp:88:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i=0; i<s.size()-2; i++) {
                ~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...