結果

問題 No.315 世界のなんとか3.5
ユーザー motimoti
提出日時 2015-12-15 18:51:45
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 238 ms / 2,000 ms
コード長 2,924 bytes
コンパイル時間 1,064 ms
コンパイル使用メモリ 112,064 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-13 17:09:24
合計ジャッジ時間 5,256 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 7 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 8 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 1 ms
4,352 KB
testcase_12 AC 51 ms
4,348 KB
testcase_13 AC 50 ms
4,348 KB
testcase_14 AC 99 ms
4,348 KB
testcase_15 AC 100 ms
4,352 KB
testcase_16 AC 98 ms
4,348 KB
testcase_17 AC 98 ms
4,352 KB
testcase_18 AC 51 ms
4,348 KB
testcase_19 AC 51 ms
4,352 KB
testcase_20 AC 56 ms
4,348 KB
testcase_21 AC 56 ms
4,348 KB
testcase_22 AC 101 ms
4,348 KB
testcase_23 AC 101 ms
4,352 KB
testcase_24 AC 98 ms
4,348 KB
testcase_25 AC 102 ms
4,348 KB
testcase_26 AC 98 ms
4,352 KB
testcase_27 AC 98 ms
4,352 KB
testcase_28 AC 98 ms
4,352 KB
testcase_29 AC 168 ms
4,352 KB
testcase_30 AC 168 ms
4,352 KB
testcase_31 AC 171 ms
4,348 KB
testcase_32 AC 195 ms
4,352 KB
testcase_33 AC 102 ms
4,352 KB
testcase_34 AC 144 ms
4,352 KB
testcase_35 AC 238 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <complex>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <assert.h>
#include <array>
#include <cstdio>
#include <cstring>
#include <random>
#include <functional>
#include <numeric>
#include <bitset>

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define minimize(a, x) a = std::min(a, x)
#define maximize(a, x) a = std::max(a, x)

typedef long long ll;
int const inf = 1<<29;

int const MOD = 1e9+7;

ll dp1[2][2][3][2];      // index, less, mod3, used3
ll dp2[2][2][3][2][800]; // index, less, mod3, used3, modP

int P;
string A, B;

unordered_map<int, int> simo = {{8, 3}, {80, 4}, {800, 5}};

ll f(string const& S, bool last) {

  int N = S.size();

  zero(dp2);

  if(N-simo[P] > 0) {
    zero(dp1);

    dp1[0][0][0][0] = 1;
    rep(i, N-simo[P]) {
      rep(less, 2) rep(mod3, 3) rep(used3, 2) {
        int dmax = less ? 9 : S[i] - '0';
        rep(d, dmax+1) {
          auto const& curr = dp1[i&1][less][mod3][used3];
          auto const ni = (i+1)&1;
          auto const nless = less || (d < dmax);
          auto const nmod3 = (mod3 * 10 + d) % 3;
          auto const nused3 = used3 || (d == 3);
          auto& next = dp1[ni][nless][nmod3][nused3];
          next += curr;
          next %= MOD;
        }
      }
      zero(dp1[i&1]);
    }

    rep(j, 2) rep(k, 3) rep(l, 2) {
      dp2[(N-simo[P])&1][j][k][l][0] = dp1[(N-simo[P])&1][j][k][l];
    }
  }
  else {
    dp2[0][0][0][0][0] = 1;
  }

  REP(i, max(0, N-simo[P]), N) {
    rep(less, 2) rep(mod3, 3) rep(used3, 2) rep(modP, P) {
      int dmax = less ? 9 : S[i] - '0';
      rep(d, dmax+1) {
        auto const& curr = dp2[i&1][less][mod3][used3][modP];
        auto const ni = (i+1)&1;
        auto const nless = less || (d < dmax);
        auto const nmod3 = (mod3 * 10 + d) % 3;
        auto const nused3 = used3 || (d == 3);
        auto const nmodP = (modP * 10 + d) % P;
        auto& next = dp2[ni][nless][nmod3][nused3][nmodP];
        next += curr;
        next %= MOD;
      }
    }
    zero(dp2[i&1]);
  }

  int ret = 0;
  rep(less, 2) rep(mod3, 3) rep(used3, 2) rep(modP, P) {
    if(!(mod3 == 0 || used3)) { continue; }
    if(modP == 0) { continue; }
    ret += dp2[N&1][less][mod3][used3][modP];
    ret %= MOD;
  }

  if(last) {
    rep(mod3, 3) rep(used3, 2) rep(modP, P) {
      if(!(mod3 == 0 || used3)) { continue; }
      if(modP == 0) { continue; }
      ret -= dp2[N&1][0][mod3][used3][modP];
      ret += MOD;
      ret %= MOD;
    }
  }

  return ret;
}

int main() {
  string A, B; cin >> A >> B >> P;
  cout << (f(B, false) - f(A, true) + MOD) % MOD << endl;
  return 0;
}
0