結果

問題 No.260 世界のなんとか3
ユーザー yagisumiyagisumi
提出日時 2021-04-01 05:44:50
言語 TypeScript
(5.4.3)
結果
AC  
実行時間 551 ms / 2,000 ms
コード長 3,487 bytes
コンパイル時間 9,431 ms
コンパイル使用メモリ 260,624 KB
実行使用メモリ 51,584 KB
最終ジャッジ日時 2023-08-22 11:09:09
合計ジャッジ時間 21,590 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
42,484 KB
testcase_01 AC 80 ms
42,392 KB
testcase_02 AC 81 ms
42,316 KB
testcase_03 AC 547 ms
49,624 KB
testcase_04 AC 546 ms
49,576 KB
testcase_05 AC 254 ms
49,692 KB
testcase_06 AC 231 ms
49,632 KB
testcase_07 AC 438 ms
49,704 KB
testcase_08 AC 363 ms
49,520 KB
testcase_09 AC 278 ms
49,504 KB
testcase_10 AC 473 ms
49,740 KB
testcase_11 AC 452 ms
49,656 KB
testcase_12 AC 345 ms
49,520 KB
testcase_13 AC 227 ms
49,464 KB
testcase_14 AC 427 ms
49,600 KB
testcase_15 AC 250 ms
49,520 KB
testcase_16 AC 396 ms
49,672 KB
testcase_17 AC 350 ms
49,520 KB
testcase_18 AC 344 ms
49,508 KB
testcase_19 AC 426 ms
49,520 KB
testcase_20 AC 334 ms
51,584 KB
testcase_21 AC 317 ms
49,520 KB
testcase_22 AC 427 ms
49,468 KB
testcase_23 AC 235 ms
49,636 KB
testcase_24 AC 376 ms
49,500 KB
testcase_25 AC 345 ms
49,216 KB
testcase_26 AC 320 ms
48,736 KB
testcase_27 AC 81 ms
42,472 KB
testcase_28 AC 539 ms
49,576 KB
testcase_29 AC 551 ms
49,704 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))
}

const M = 10 ** 9 + 7
function solve(nums: string, include_max: boolean) {
  const max_state = {
    mod3: 0,
    has3: false,
    mod8: 0,
  }
  let dp = array3(3, 2, 8, 0)
  for (let i = 0; i < nums.length; i++) {
    const dp2 = array3(3, 2, 8, 0)
    const digit = Number(nums[i])

    for (let j = 0; j < digit; j++) {
      const m3 = (max_state.mod3 * 10 + j) % 3
      const m8 = (max_state.mod8 * 10 + j) % 8
      const has3 = max_state.has3 || j === 3 ? 1 : 0
      dp2[m3][has3][m8] += 1
    }

    max_state.mod3 = (max_state.mod3 * 10 + digit) % 3
    max_state.mod8 = (max_state.mod8 * 10 + digit) % 8
    max_state.has3 = max_state.has3 || 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 || j === 3 ? 1 : 0
            const t8 = (m8 * 10 + j) % 8
            dp2[t3][h3][t8] = (dp2[t3][h3][t8] + dp[m3][has3][m8]) % M
          }
        }
      }
    }

    dp = dp2
  }

  let r = 0
  if (include_max && (max_state.mod3 === 0 || max_state.has3) && max_state.mod8 !== 0) {
    r += 1
  }
  for (let i = 1; i < 8; i++) {
    r = (r + dp[0][0][i]) % M
    r = (r + dp[0][1][i]) % M
    r = (r + dp[1][1][i]) % M
    r = (r + dp[2][1][i]) % M
  }

  return r
}

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

  const A = input.word()
  const B = input.word()

  const r = (solve(B, true) - solve(A, false) + M) % M
  console.log(r)
}

main()
0