結果

問題 No.260 世界のなんとか3
ユーザー nominomi
提出日時 2019-02-16 16:57:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 203 ms / 2,000 ms
コード長 3,909 bytes
コンパイル時間 1,913 ms
コンパイル使用メモリ 173,328 KB
実行使用メモリ 18,732 KB
最終ジャッジ日時 2023-10-23 20:35:21
合計ジャッジ時間 5,676 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 128 ms
18,716 KB
testcase_04 AC 128 ms
18,716 KB
testcase_05 AC 61 ms
9,188 KB
testcase_06 AC 39 ms
7,216 KB
testcase_07 AC 122 ms
16,472 KB
testcase_08 AC 140 ms
16,712 KB
testcase_09 AC 68 ms
10,104 KB
testcase_10 AC 100 ms
15,224 KB
testcase_11 AC 131 ms
17,416 KB
testcase_12 AC 109 ms
14,148 KB
testcase_13 AC 20 ms
5,668 KB
testcase_14 AC 125 ms
16,588 KB
testcase_15 AC 26 ms
6,436 KB
testcase_16 AC 98 ms
14,072 KB
testcase_17 AC 71 ms
11,388 KB
testcase_18 AC 60 ms
10,444 KB
testcase_19 AC 131 ms
16,568 KB
testcase_20 AC 80 ms
11,736 KB
testcase_21 AC 94 ms
12,548 KB
testcase_22 AC 97 ms
14,444 KB
testcase_23 AC 11 ms
4,616 KB
testcase_24 AC 120 ms
15,484 KB
testcase_25 AC 203 ms
18,728 KB
testcase_26 AC 123 ms
18,732 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 123 ms
18,720 KB
testcase_29 AC 123 ms
18,716 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define vvi vector<vector<int>>
#define vec vector
#define pq priority_queue
#define all(v) (v).begin(), (v).end()
#define uniqueV(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
#define rep(i, n) for (int (i) = (0); (i) < (n); ++(i))
#define repp(i, m, n) for (int (i) = (m); (i) < (n); ++(i))
#define dbg(x) cerr << #x << ": " << x << endl;
#define dbg2(x, y) cerr<<"("<<#x<<", "<<#y<<") = "<<"("<<x<<", "<<y<<")"<<endl;
#define dbg3(x, y, z) cerr<<"("<<#x<<", "<<#y<<", "<<#z<<") = "<<"("<<x<<", "<<y<<", "<<z<<")"<<endl;
#define dbgB(value, size) cerr<<#value<<": "<<bitset<size>(value) << endl;
#define line() cerr << "---------------" << endl;

const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};
const double PI = 3.14159265358979323846;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }

template<typename T>
void print1(T begin, T end) {
  while (begin != end) {
    cout << (*begin) << " ";
    *begin++;
  }
  cout << endl;
}

template<typename T>
void print2(T Array, int height, int width) {
  for (int i = 0; i < height; i++) {
    for (int j = 0; j < width; j++) {
      cout << " " << Array[i][j];
    }
    cout << endl;
  }
}

void print() { std::cerr << endl; }
template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
  std::cerr << head << " ";
  print(std::forward<Tail>(tail)...);
}

template<class T>
void Add(T &a, const T &b, const T &mod=1000000007) {
  int val = ((a % mod) + (b % mod)) % mod;
  if (val < 0) { val += mod; }
  a = val;
}

// ------------------------------------------------------------------------------------------

string A, B;
int dp[11111][2][2][2][3][8];
int a[111];

int rec(int digit, int over, int smaller, int three, int mod3, int mod8) {
  if (digit >= B.size()) {
    return (three || mod3 == 0) && (mod8 != 0);
  }

  if (dp[digit][over][smaller][three][mod3][mod8] != -1) {
    return dp[digit][over][smaller][three][mod3][mod8];
  }

  int ue = (smaller ? 9 : B[digit] - '0');
  int shita = (over ? 0 : A[digit] - '0');

  int ret = 0;
  for (int num = shita; num <= ue; num++) {
    int t = rec(digit+1, over || (num > shita), smaller || (num < ue), three || (num == 3), (mod3+num)%3, (10*mod8+num)%8);
    Add(ret, t);
  }

  return dp[digit][over][smaller][three][mod3][mod8] = ret;
}

signed main() {
  cin >> A >> B;
  int diff = B.size() - A.size();
  string zero = "";
  rep (i, diff) {
    zero += "0";
  }
  A = zero + A;

  // rep (i, 11111) rep (j, 2) rep (k, 2) rep (l, 2) rep (m, 3) rep (n, 8) dp[i][j][k][l][m][n] = -1;

  // int ans = rec(0, 0, 0, 0, 0, 0);

  dp[0][0][0][0][0][0] = 1;
  for (int digit = 0; digit < B.size(); digit++) {
    for (int over : {0, 1}) {
      for (int less : {0, 1}) {
        for (int three : {0, 1}) {
          for (int mod3 = 0; mod3 < 3; mod3++) {
            for (int mod8 = 0; mod8 < 8; mod8++) {
              int ue = (less ? 9 : B[digit] - '0');
              int shita = (over ? 0 : A[digit] - '0');
              for (int num = shita; num <= ue; num++) {
                Add(dp[digit+1][over || (num > shita)][less || (num < ue)][three || (num == 3)][(mod3+num)%3][(10*mod8+num)%8],
                dp[digit][over][less][three][mod3][mod8]);
              }
            }
          }
        }
      }
    }
  }

  int ans = 0;
  for (int over : {0, 1}) {
    for (int less : {0, 1}) {
      for (int three : {0, 1}) {
        for (int mod3 = 0; mod3 < 3; mod3++) {
          for (int mod8 = 0; mod8 < 8; mod8++) {
            if ((three || mod3 == 0) && (mod8 != 0)) {
              Add(ans, dp[B.size()][over][less][three][mod3][mod8]);
            }
          }
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}
0