結果

問題 No.319 happy b1rthday 2 me
ユーザー ccppjsrbccppjsrb
提出日時 2023-04-19 00:36:56
言語 Go
(1.22.1)
結果
WA  
実行時間 -
コード長 3,164 bytes
コンパイル時間 10,255 ms
コンパイル使用メモリ 220,788 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-22 01:47:06
合計ジャッジ時間 11,141 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 1 ms
5,376 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 WA -
testcase_17 AC 1 ms
5,376 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 WA -
testcase_23 AC 1 ms
5,376 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 1 ms
5,376 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 1 ms
5,376 KB
testcase_30 AC 1 ms
5,376 KB
testcase_31 AC 1 ms
5,376 KB
testcase_32 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

var iost *Iost

type Iost struct {
	Scanner *bufio.Scanner
	Writer  *bufio.Writer
}

func NewIost(fp io.Reader, wfp io.Writer) *Iost {
	const BufSize = 2000005
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, BufSize), BufSize)
	return &Iost{Scanner: scanner, Writer: bufio.NewWriter(wfp)}
}
func (i *Iost) Text() string {
	if !i.Scanner.Scan() {
		panic("scan failed")
	}
	return i.Scanner.Text()
}
func (i *Iost) Atoi(s string) int                 { x, _ := strconv.Atoi(s); return x }
func (i *Iost) GetNextInt() int                   { return i.Atoi(i.Text()) }
func (i *Iost) Atoi64(s string) int64             { x, _ := strconv.ParseInt(s, 10, 64); return x }
func (i *Iost) GetNextInt64() int64               { return i.Atoi64(i.Text()) }
func (i *Iost) Atof64(s string) float64           { x, _ := strconv.ParseFloat(s, 64); return x }
func (i *Iost) GetNextFloat64() float64           { return i.Atof64(i.Text()) }
func (i *Iost) Print(x ...interface{})            { fmt.Fprint(i.Writer, x...) }
func (i *Iost) Printf(s string, x ...interface{}) { fmt.Fprintf(i.Writer, s, x...) }
func (i *Iost) Println(x ...interface{})          { fmt.Fprintln(i.Writer, x...) }
func isLocal() bool                               { return os.Getenv("NICKEL") == "BACK" }
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	if isLocal() {
		fp, _ = os.Open(os.Getenv("WELL_EVERYBODY_LIES_TOO_MUCH"))
	}
	iost = NewIost(fp, wfp)
	defer func() {
		iost.Writer.Flush()
	}()
	solve()
}
func solve() {
	l := iost.GetNextInt()
	r := iost.GetNextInt()
	iost.Println(f(r) - f(l-1) + g(r-1) - g(l))
}
func f(r int) int {
	rr := make([]int, 0)
	for r > 0 {
		rr = append(rr, r%10)
		r /= 10
	}
	a := 0
	n := len(rr)
	b := n - 1
	for a < b {
		rr[a], rr[b] = rr[b], rr[a]
		a++
		b--
	}
	dp := make([][2][2]int, n+1)
	cnt := make([][2][2]int, n+1)
	cnt[n][0][0] = 1
	ans := 0
	for i := n - 1; i >= 0; i-- {
		for j := 0; j < 2; j++ {
			for k := 0; k < 2; k++ {
				for d := 0; d < 10; d++ {
					tj := 0
					if rr[i]-j < d {
						tj = 1
					}
					tk := 0
					if d == 2 {
						tk = 1
					}
					add := 0
					if d == 1 && k == 1 {
						add = cnt[i+1][j][k]
					}
					dp[i][tj][tk] += dp[i+1][j][k] + add
					cnt[i][tj][tk] += cnt[i+1][j][k]
					if d == 0 {
						continue
					}
					if i > 0 || tj == 0 {
						ans += dp[i+1][j][k] + add
					}
				}
			}
		}
	}
	return ans
}
func g(r int) int {
	if r == 0 {
		return 0
	}
	rr := make([]int, 0)
	for r > 0 {
		rr = append(rr, r%10)
		r /= 10
	}
	a := 0
	n := len(rr)
	b := n - 1
	for a < b {
		rr[a], rr[b] = rr[b], rr[a]
		a++
		b--
	}
	dp := make([][2]int, n+1)
	enable := make([][2]int, n)
	enable[n-1][0] = 1
	enable[n-1][1] = 1
	for i := 0; i < n-1; i++ {
		enable[i][1] = 9
	}
	dp[n][0] = 1
	ans := 0
	for i := n - 1; i >= 0; i-- {
		for j := 0; j < 2; j++ {
			for d := enable[i][0]; d <= enable[i][1]; d++ {
				tj := 0
				if rr[i]-j < d {
					tj = 1
				}
				dp[i][tj] += dp[i+1][j]
				if d != 2 {
					continue
				}
				if i > 0 || tj == 0 {
					ans += dp[i+1][j]
				}
			}
		}
	}
	return ans
}
0