結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
20,380 KB
testcase_01 AC 6 ms
20,376 KB
testcase_02 AC 7 ms
20,380 KB
testcase_03 AC 71 ms
21,972 KB
testcase_04 AC 73 ms
21,972 KB
testcase_05 AC 31 ms
20,972 KB
testcase_06 AC 22 ms
20,760 KB
testcase_07 AC 62 ms
21,744 KB
testcase_08 AC 63 ms
21,780 KB
testcase_09 AC 35 ms
21,072 KB
testcase_10 AC 57 ms
21,604 KB
testcase_11 AC 66 ms
21,844 KB
testcase_12 AC 52 ms
21,496 KB
testcase_13 AC 16 ms
20,588 KB
testcase_14 AC 63 ms
21,756 KB
testcase_15 AC 18 ms
20,668 KB
testcase_16 AC 51 ms
21,484 KB
testcase_17 AC 39 ms
21,196 KB
testcase_18 AC 36 ms
21,100 KB
testcase_19 AC 61 ms
21,756 KB
testcase_20 AC 40 ms
21,236 KB
testcase_21 AC 45 ms
21,328 KB
testcase_22 AC 53 ms
21,524 KB
testcase_23 AC 11 ms
20,476 KB
testcase_24 AC 57 ms
21,644 KB
testcase_25 AC 72 ms
21,988 KB
testcase_26 AC 70 ms
21,992 KB
testcase_27 AC 7 ms
20,316 KB
testcase_28 AC 8 ms
21,976 KB
testcase_29 AC 7 ms
21,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define int long long
#define rep(i, n) for (int (i) = (0); (i) < (n); ++(i))

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]; // dp[桁][Aを超えるか][B未満か][3を含むか][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;
  // Aの桁数が小さい時、Aの上位桁を0で埋める
  int diff = B.size() - A.size();
  string zero = "";
  rep (i, diff) {
    zero += "0";
  }
  A = zero + A;

  // DPテーブル初期化
  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);
  cout << ans << endl;

  return 0;
}
0