結果

問題 No.260 世界のなんとか3
ユーザー yagisumiyagisumi
提出日時 2021-04-01 16:24:05
言語 TypeScript
(5.4.3)
結果
WA  
実行時間 -
コード長 4,833 bytes
コンパイル時間 9,182 ms
コンパイル使用メモリ 258,996 KB
実行使用メモリ 50,744 KB
最終ジャッジ日時 2023-08-22 19:09:31
合計ジャッジ時間 17,495 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
45,504 KB
testcase_01 AC 77 ms
45,572 KB
testcase_02 AC 79 ms
45,612 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 78 ms
42,492 KB
testcase_28 AC 270 ms
48,144 KB
testcase_29 AC 283 ms
48,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import { SSL_OP_LEGACY_SERVER_CONNECT } from 'constants'
import * as fs from 'fs'

// import * as readline from 'readline'
// const rl = readline.createInterface({ input: process.stdin, output: process.stdout })
// const ask = (query: string) => new Promise<string>((resolve) => rl.question(query, resolve))
// // Don't forget `rl.close()`.

const INT = Math.floor

declare global {
  interface Array<T> {
    last(): T | undefined
    isEmpty(): boolean
  }
}
Array.prototype.last = function () {
  return this.length === 0 ? undefined : this[this.length - 1]
}
Array.prototype.isEmpty = function () {
  return this.length === 0
}

const bigIntMax = (...args: bigint[]) => args.reduce((m, e) => (e > m ? e : m))
const bigIntMin = (...args: bigint[]) => args.reduce((m, e) => (e < m ? e : m))
const bigIntAbs = (arg: bigint) => (arg < 0 ? -arg : arg)

declare const stdin: number
function read_stdin() {
  return fs.readFileSync(process.env.NODE_ENV === 'debug' ? stdin : process.stdin.fd, 'utf8')
}
class Input {
  readonly inputs: string[]
  private index = 0
  constructor(str?: string) {
    this.inputs = (str ? str : read_stdin()).split(/\s+/)
  }
  number() {
    return Number(this.inputs[this.index++])
  }
  numbers(n: number) {
    return this.inputs.slice(this.index, (this.index += n)).map(Number)
  }
  bigint() {
    return BigInt(this.inputs[this.index++])
  }
  bigints(n: number) {
    return this.inputs.slice(this.index, (this.index += n)).map(BigInt)
  }
  word() {
    return this.inputs[this.index++]
  }
  words(n: number) {
    return this.inputs.slice(this.index, (this.index += n))
  }
}

function array<T>(len: number, init: T): T[] {
  return Array(len).fill(init)
}

function array2<T>(h: number, w: number, init: T): T[][] {
  return array(h, 0).map(() => array(w, init))
}

function array3<T>(d: number, h: number, w: number, init: T): T[][][] {
  return array(d, 0).map(() => array2(h, w, init))
}

function IDX(mod3: number, has3: boolean, mod8: number) {
  return mod3 * 2 * 8 + (has3 ? 1 : 0) * 8 + mod8
}

function main() {
  const input = new Input()

  const A_ = input.word()
  const B = input.word()
  const A = A_.padStart(B.length, '0')
  const M = 10 ** 9 + 7

  const b_max_state = {
    mod3: 0,
    has3: false,
    mod8: 0,
  }
  const a_max_state = {
    mod3: 0,
    has3: false,
    mod8: 0,
  }
  let b_dp = Array(3 * 2 * 8).fill(0)
  let b_dp2 = Array(3 * 2 * 8).fill(0)
  let a_dp = Array(3 * 2 * 8).fill(0)
  let a_dp2 = Array(3 * 2 * 8).fill(0)
  let b_count = 0
  let a_count = 0
  for (let i = 0; i < B.length; i++) {
    b_dp2.fill(0)
    a_dp2.fill(0)
    const b_digit = Number(B[i])
    const a_digit = Number(A[i])

    for (let j = 0; j < b_digit; j++) {
      const m3 = (b_max_state.mod3 * 10 + j) % 3
      const m8 = (b_max_state.mod8 * 10 + j) % 8
      const has3 = b_max_state.has3 || j === 3
      b_dp2[IDX(m3, has3, m8)] += 1
      if (i === B.length - 1) {
        if ((m3 === 0 || has3) && m8 !== 0) {
          b_count += 1
        }
      }
    }

    for (let j = 0; j < a_digit; j++) {
      const m3 = (a_max_state.mod3 * 10 + j) % 3
      const m8 = (a_max_state.mod8 * 10 + j) % 8
      const has3 = a_max_state.has3 || j === 3
      a_dp2[IDX(m3, has3, m8)] += 1
      if (i === B.length - 1) {
        if ((m3 === 0 || has3) && m8 !== 0) {
          a_count += 1
        }
      }
    }

    b_max_state.mod3 = (b_max_state.mod3 * 10 + b_digit) % 3
    b_max_state.mod8 = (b_max_state.mod8 * 10 + b_digit) % 8
    b_max_state.has3 = b_max_state.has3 || b_digit === 3
    a_max_state.mod3 = (a_max_state.mod3 * 10 + a_digit) % 3
    a_max_state.mod8 = (a_max_state.mod8 * 10 + a_digit) % 8
    a_max_state.has3 = a_max_state.has3 || a_digit === 3

    for (let m3 = 0; m3 < 3; m3++) {
      for (let has3 = 0; has3 < 2; has3++) {
        for (let m8 = 0; m8 < 8; m8++) {
          for (let j = 0; j < 10; j++) {
            const t3 = (m3 * 10 + j) % 3
            const h3 = has3 === 1 || j === 3
            const t8 = (m8 * 10 + j) % 8
            const idx = IDX(t3, h3, t8)
            b_dp2[idx] += b_dp[IDX(m3, has3 === 1, m8)]
            a_dp2[idx] += a_dp[IDX(m3, has3 === 1, m8)]
            if ((i & 0b1111) === 0) {
              b_dp2[idx] %= M
              a_dp2[idx] %= M
            }
            if (i === B.length - 1) {
              if ((t3 === 0 || h3) && t8 !== 0) {
                b_count = (b_count + b_dp[IDX(m3, has3 === 1, m8)]) % M
                a_count = (a_count + a_dp[IDX(m3, has3 === 1, m8)]) % M
              }
            }
          }
        }
      }
    }

    ;[b_dp, b_dp2, a_dp, a_dp2] = [b_dp2, b_dp, a_dp2, a_dp]
  }

  if ((b_max_state.mod3 === 0 || b_max_state.has3) && b_max_state.mod8 !== 0) {
    b_count += 1
  }

  const r = (b_count - a_count) % M
  console.log(r < 0 ? r + M : r)
}

main()
0