結果

問題 No.315 世界のなんとか3.5
ユーザー Min_25Min_25
提出日時 2015-12-08 14:45:23
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 8 ms / 2,000 ms
コード長 2,363 bytes
コンパイル時間 480 ms
コンパイル使用メモリ 67,060 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-12 21:55:12
合計ジャッジ時間 2,082 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 3 ms
4,352 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 3 ms
4,348 KB
testcase_07 AC 1 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 2 ms
4,352 KB
testcase_12 AC 3 ms
4,348 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 4 ms
4,348 KB
testcase_15 AC 4 ms
4,348 KB
testcase_16 AC 4 ms
4,348 KB
testcase_17 AC 4 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 3 ms
4,348 KB
testcase_20 AC 4 ms
4,352 KB
testcase_21 AC 4 ms
4,348 KB
testcase_22 AC 5 ms
4,352 KB
testcase_23 AC 5 ms
4,352 KB
testcase_24 AC 4 ms
4,348 KB
testcase_25 AC 6 ms
4,348 KB
testcase_26 AC 4 ms
4,348 KB
testcase_27 AC 4 ms
4,352 KB
testcase_28 AC 6 ms
4,348 KB
testcase_29 AC 8 ms
4,348 KB
testcase_30 AC 6 ms
4,352 KB
testcase_31 AC 6 ms
4,348 KB
testcase_32 AC 7 ms
4,352 KB
testcase_33 AC 6 ms
4,348 KB
testcase_34 AC 8 ms
4,348 KB
testcase_35 AC 8 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cassert>
#include <cstring>
#include <ctime>

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <utility>

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

using namespace std;
using int64 = long long;

char A[200010];
char B[200010];

const int MOD = 1e9 + 7;

int pow_mod(int b, int e, int m) {
  int ret = 1;
  while (e) {
    if (e & 1) ret = int64(ret) * b % m;
    b = int64(b) * b % m;
    e >>= 1;
  }
  return ret;
}

template <typename T, typename U>
void add(T& a, U b) {
  a += b;
  if (a >= MOD) a -= MOD;
}

int count(char* str, int size, int p, int t, bool inclusive) {
  int64 dp[2][3200];

  int64 prefix = 0;
  bool has3 = false;

  int64 f = 0;
  int mid = max(0, size - 3 - t - 1);
  int64 s = 0;
  rep(i, mid) {
    int d = str[i] - '0';
    f = (f * 10 + s) % MOD;
    s = s * 9 % MOD;
    if (has3) add(f, d);
    else {
      add(s, d - (d > 3));
      add(f, d > 3);
    }
    prefix = (prefix + d) % 3;
    has3 |= d == 3;
  }

  // 3 * p
  const int mod = 3 * p;
  int64* curr = dp[0], *next = dp[1];
  fill(curr, curr + 4 * p, 0);

  curr[0] = curr[1] = curr[2] = s * pow_mod(3, MOD - 2, MOD) % MOD;
  curr[mod] = f % MOD;
  rep3(i, mid, size) {
    int d = str[i] - '0';
    fill(next, next + 4 * p, 0);
    rep(r, mod) rep(j, 10) {
      if (j == 3) add(next[mod + (r * 10 + j) % p], curr[r]);
      else add(next[(r * 10 + j) % mod], curr[r]);
    }
    rep(r, p) rep(j, 10) add(next[mod + (r * 10 + j) % p], curr[mod + r]);
    rep(j, d) {
      if (j == 3 || has3) add(next[mod + (prefix * 10 + j) % p], 1);
      else add(next[(prefix * 10 + j) % mod], 1);
    }
    prefix = (prefix * 10 + d) % mod;
    has3 |= d == 3;
    swap(curr, next);
  }
  if (inclusive) {
    if (has3) add(curr[mod + prefix % p], 1);
    else add(curr[prefix], 1);
  }
  int64 ret = 0;
  rep(i, mod) if (i % 3 == 0 && i % p != 0) ret += curr[i];
  rep3(i, 1, p) ret += curr[i + mod];
  return ret % MOD;
}

void solve() {
  int P;
  while (~scanf("%s %s %d", A, B, &P)) {
    int t = 0;
    int Q = P;
    while (Q % 10 == 0) Q /= 10, ++t;
    int ans = (MOD - count(A, strlen(A), P, t, false)
                   + count(B, strlen(B), P, t, true)) % MOD;
    printf("%d\n", ans);
  }
}

int main() {
  solve();
  return 0;
}
0