結果

問題 No.260 世界のなんとか3
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-01-21 10:26:30
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 143 ms / 2,000 ms
コード長 2,621 bytes
コンパイル時間 658 ms
コンパイル使用メモリ 108,748 KB
実行使用メモリ 19,188 KB
最終ジャッジ日時 2023-09-03 22:50:45
合計ジャッジ時間 4,734 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
15,908 KB
testcase_01 AC 70 ms
17,160 KB
testcase_02 AC 71 ms
15,888 KB
testcase_03 AC 112 ms
17,520 KB
testcase_04 AC 112 ms
17,532 KB
testcase_05 AC 91 ms
16,484 KB
testcase_06 AC 82 ms
17,196 KB
testcase_07 AC 111 ms
17,292 KB
testcase_08 AC 117 ms
18,708 KB
testcase_09 AC 92 ms
17,868 KB
testcase_10 AC 102 ms
17,312 KB
testcase_11 AC 117 ms
17,548 KB
testcase_12 AC 108 ms
17,004 KB
testcase_13 AC 76 ms
17,468 KB
testcase_14 AC 111 ms
18,132 KB
testcase_15 AC 80 ms
16,180 KB
testcase_16 AC 103 ms
17,032 KB
testcase_17 AC 95 ms
16,680 KB
testcase_18 AC 93 ms
16,732 KB
testcase_19 AC 116 ms
17,272 KB
testcase_20 AC 99 ms
16,748 KB
testcase_21 AC 105 ms
18,220 KB
testcase_22 AC 103 ms
18,448 KB
testcase_23 AC 74 ms
16,240 KB
testcase_24 AC 112 ms
18,672 KB
testcase_25 AC 143 ms
17,524 KB
testcase_26 AC 107 ms
18,728 KB
testcase_27 AC 72 ms
15,884 KB
testcase_28 AC 73 ms
17,468 KB
testcase_29 AC 73 ms
19,188 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; }

void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

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;
enum M = 24;
enum LIM = 10^^4 + 10;

int[] ten;
long[][][] dp;

int N;
string A, B;

long calc(int i, int x, int y, bool diffA, bool diffB) {
  long ret;
  if (i == N) {
    ret += ((x % 3 == 0 || y) && (x % 8 != 0)) ? 1 : 0;
  } else if (diffA && diffB) {
    foreach (dx; 0 .. M) foreach (dy; 0 .. 2) {
      const xx = (x * ten[N - i] + dx) % M;
      const yy = y | dy;
      if ((xx % 3 == 0 || yy) && (xx % 8 != 0)) {
        ret += dp[N - i][dx][dy];
      }
    }
  } else {
    foreach (d; (diffA ? 0 : (A[i] - '0')) .. (diffB ? 9 : (B[i] - '0')) + 1) {
      ret += calc(i + 1, (x * 10 + d) % M, (y || d == 3) ? 1 : 0, (diffA || (d != A[i] - '0')), (diffB || (d != B[i] - '0')));
    }
  }
  ret %= MO;
  debug {
    writefln("calc %s %s %s %s %s: %s", i, x, y, diffA, diffB, ret);
  }
  return ret;
}

void main() {
  ten = new int[LIM];
  ten[0] = 1;
  foreach (i; 1 .. LIM) {
    ten[i] = (ten[i - 1] * 10) % M;
  }
  
  dp = new long[][][](LIM, M, 2);
  dp[0][0][0] = 1;
  foreach (i; 1 .. LIM) {
    foreach (x; 0 .. M) foreach (y; 0 .. 2) {
      foreach (d; 0 .. 10) {
        (dp[i][(x * 10 + d) % M][(y || d == 3) ? 1 : 0] += dp[i - 1][x][y]) %= MO;
      }
    }
  }
  
  try {
    for (; ; ) {
      A = readToken();
      B = readToken();
      N = cast(int)(B.length);
      A = iota(N - A.length).map!(_ => '0').array.to!string ~ A;
      debug {
        writeln("A = ", A);
        writeln("B = ", B);
      }
      const ans = calc(0, 0, 0, false, false);
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0