답안 #679597

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
679597 2023-01-08T15:48:19 Z peijar Palindrome-Free Numbers (BOI13_numbers) C++17
100 / 100
1 ms 340 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;

namespace std {
template <typename T> ostream &operator<<(ostream &out, const vector<T> &vec) {
  out << "[";
  for (int i = 0; i < (int)vec.size(); ++i) {
    out << vec[i];
    if (i + 1 < (int)vec.size())
      out << ", ";
  }
  return out << "]";
}
} // namespace std

void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
  cout << ' ' << H;
  dbg_out(T...);
}

#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

const int MAXLEN = 20;
const int MAXK = 11;
int ways[MAXLEN][MAXK][MAXK][2]; // dp[digRestant][prv][prv2][flag]

int cntSmaller(string s) {
  reverse(s.begin(), s.end());
  for (int bitRestant = 0; bitRestant <= (int)s.size(); ++bitRestant)
    for (int prv = 0; prv < MAXK; ++prv)
      for (int prv2 = 0; prv2 < MAXK; ++prv2)
        for (int flag = 0; flag < 2; ++flag) {
          int &cur = ways[bitRestant][prv][prv2][flag];
          cur = 0;
          if (bitRestant == 0) {
            cur = 1;
            continue;
          }
          for (int nxtDig = prv == 10; nxtDig < 10; ++nxtDig)
            if (nxtDig != prv and nxtDig != prv2) {
              if (!flag and nxtDig > s[bitRestant - 1] - '0')
                continue;
              cur += ways[bitRestant - 1][nxtDig][prv]
                         [flag or (nxtDig < s[bitRestant - 1] - '0')];
            }
        }
  int ret = 0;
  for (int len = 0; len <= (int)s.size(); ++len)
    ret += ways[len][10][10][len < (int)s.size()];
  dbg(s, ret);
  return ret;
}

signed main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int a, b;
  cin >> a >> b;
  int ret = cntSmaller(to_string(b));
  dbg(ret);
  if (a)
    ret -= cntSmaller(to_string(a - 1));
  cout << ret << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 328 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 328 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 324 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 324 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 328 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 328 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 324 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 324 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 324 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 328 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 332 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 324 KB Output is correct
45 Correct 1 ms 340 KB Output is correct