結果

問題 No.260 世界のなんとか3
ユーザー nominomi
提出日時 2019-02-17 11:05:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 281 ms / 2,000 ms
コード長 1,927 bytes
コンパイル時間 1,804 ms
コンパイル使用メモリ 172,876 KB
実行使用メモリ 18,680 KB
最終ジャッジ日時 2023-10-25 22:11:41
合計ジャッジ時間 6,491 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 172 ms
18,668 KB
testcase_04 AC 172 ms
18,668 KB
testcase_05 AC 82 ms
9,140 KB
testcase_06 AC 53 ms
7,164 KB
testcase_07 AC 164 ms
16,424 KB
testcase_08 AC 191 ms
16,664 KB
testcase_09 AC 93 ms
10,056 KB
testcase_10 AC 133 ms
15,172 KB
testcase_11 AC 177 ms
17,368 KB
testcase_12 AC 146 ms
14,096 KB
testcase_13 AC 25 ms
5,620 KB
testcase_14 AC 169 ms
16,540 KB
testcase_15 AC 35 ms
6,384 KB
testcase_16 AC 133 ms
14,020 KB
testcase_17 AC 96 ms
11,336 KB
testcase_18 AC 80 ms
10,396 KB
testcase_19 AC 178 ms
16,520 KB
testcase_20 AC 108 ms
11,688 KB
testcase_21 AC 126 ms
12,500 KB
testcase_22 AC 131 ms
14,396 KB
testcase_23 AC 13 ms
4,568 KB
testcase_24 AC 164 ms
15,432 KB
testcase_25 AC 281 ms
18,680 KB
testcase_26 AC 163 ms
18,680 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 162 ms
18,668 KB
testcase_29 AC 162 ms
18,668 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];

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

  // DPテーブルを初期化
  dp[0][0][0][0][0][0] = 1;
  // DP
  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++) {
                int nLess = less || (num < ue);
                int nOver = over || (num > shita);
                if (less == 1 && nLess == 0) continue;
                if (over == 1 && nOver == 0) continue;
                Add(dp[digit+1][nOver][nLess][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