結果

問題 No.260 世界のなんとか3
ユーザー yagisumiyagisumi
提出日時 2021-04-01 05:44:50
言語 TypeScript
(5.4.3)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,487 bytes
コンパイル時間 6,776 ms
コンパイル使用メモリ 147,484 KB
最終ジャッジ日時 2024-05-09 16:48:54
合計ジャッジ時間 7,186 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.ts(1,46): error TS2307: Cannot find module 'constants' or its corresponding type declarations.
main.ts(2,21): error TS2307: Cannot find module 'fs' or its corresponding type declarations.
main.ts(30,26): error TS2580: Cannot find name 'process'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.
main.ts(30,69): error TS2580: Cannot find name 'process'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.

ソースコード

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