結果

問題 No.315 世界のなんとか3.5
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-01-24 15:05:55
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 1,122 ms / 2,000 ms
コード長 3,268 bytes
コンパイル時間 754 ms
コンパイル使用メモリ 110,784 KB
実行使用メモリ 113,420 KB
最終ジャッジ日時 2023-09-03 23:02:59
合計ジャッジ時間 16,292 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
31,280 KB
testcase_01 AC 49 ms
30,776 KB
testcase_02 AC 50 ms
30,776 KB
testcase_03 AC 48 ms
30,796 KB
testcase_04 AC 48 ms
30,776 KB
testcase_05 AC 50 ms
30,792 KB
testcase_06 AC 50 ms
30,836 KB
testcase_07 AC 51 ms
30,788 KB
testcase_08 AC 50 ms
30,708 KB
testcase_09 AC 47 ms
30,764 KB
testcase_10 AC 48 ms
30,740 KB
testcase_11 AC 48 ms
30,788 KB
testcase_12 AC 583 ms
71,208 KB
testcase_13 AC 583 ms
70,888 KB
testcase_14 AC 472 ms
71,524 KB
testcase_15 AC 473 ms
71,188 KB
testcase_16 AC 472 ms
71,520 KB
testcase_17 AC 473 ms
70,956 KB
testcase_18 AC 583 ms
70,820 KB
testcase_19 AC 587 ms
71,028 KB
testcase_20 AC 584 ms
71,004 KB
testcase_21 AC 585 ms
71,220 KB
testcase_22 AC 471 ms
70,784 KB
testcase_23 AC 471 ms
70,900 KB
testcase_24 AC 468 ms
71,356 KB
testcase_25 AC 474 ms
71,340 KB
testcase_26 AC 471 ms
71,076 KB
testcase_27 AC 471 ms
70,864 KB
testcase_28 AC 191 ms
70,864 KB
testcase_29 AC 605 ms
112,164 KB
testcase_30 AC 605 ms
111,544 KB
testcase_31 AC 600 ms
112,148 KB
testcase_32 AC 900 ms
112,188 KB
testcase_33 AC 1,122 ms
113,420 KB
testcase_34 AC 297 ms
112,204 KB
testcase_35 AC 300 ms
111,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.numeric, std.range, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = cast(int)(as.length); for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a < val)); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a <= val)); }


enum MO = 10L^^9 + 7;

int N;
string A, B;
int P;

long[][][][] small;
long[][][] dp;

long calc(int i, int x, int y, int z, bool diffA, bool diffB) {
  long ret;
  if (i == N || diffA && diffB) {
    foreach (x0; 0 .. 3) foreach (y0; 0 .. 2) {
      foreach (x1; 0 .. 3) foreach (y1; 0 .. 2) {
        if ((x0 + x1 + x) % 3 == 0 || (y0 || y1 || y)) {
          ret += small[min(N - i, 5)][x0][y0][z] * dp[max(N - 5 - i, 0)][x1][y1];
        }
      }
    }
  } else {
    foreach (d; (diffA ? 0 : (A[i] - '0')) .. (diffB ? 9 : (B[i] - '0')) + 1) {
      int zz = z;
      if (N - 1 - i < 5) {
        zz += 10^^(N - 1 - i) * d;
      }
      ret += calc(i + 1, (x + d) % 3, (y || d == 3) ? 1 : 0, zz, (diffA || (d != A[i] - '0')), (diffB || (d != B[i] - '0')));
    }
  }
  ret %= MO;
  debug {
    writefln("calc %s %s %s %s %s %s: %s", i, x, y, z, diffA, diffB, ret);
  }
  return ret;
}

void main() {
  try {
    for (; ; ) {
      A = readToken();
      B = readToken();
      P = readInt();
      
      A = iota(B.length - A.length).map!(_ => '0').array.to!string ~ A;
      N = cast(int)(B.length);
      
      small = new long[][][][](6, 3, 2, 10^^5);
      foreach (i; 0 .. 5 + 1) {
        foreach (z; 0 .. 10^^5) {
          if (z % 10^^i == 0) {
            foreach (w; 0 .. 10^^i) {
              if ((z + w) % P != 0) {
                const x = w % 3;
                const y = iota(i).map!(j => (w / 10^^j % 10 == 3)).any;
                ++small[i][x][y][z];
              }
            }
          }
        }
      }
      
      dp = new long[][][](N + 1, 3, 2);
      dp[0][0][0] = 1;
      foreach (i; 0 .. N) {
        foreach (x; 0 .. 3) foreach (y; 0 .. 2) {
          foreach (d; 0 .. 10) {
            const xx = (x + d) % 3;
            const yy = (y || d == 3) ? 1 : 0;
            if ((dp[i + 1][xx][yy] += dp[i][x][y]) >= MO) {
              dp[i + 1][xx][yy] -= MO;
            }
          }
        }
      }
      debug {
        writeln("dp = ", dp[0 .. min(10, $)]);
      }
      
      const ans = calc(0, 0, 0, 0, false, false);
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0