結果

問題 No.997 Jumping Kangaroo
ユーザー ccppjsrbccppjsrb
提出日時 2020-06-03 22:38:33
言語 Go
(1.22.1)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 3,565 bytes
コンパイル時間 15,595 ms
コンパイル使用メモリ 212,020 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-17 10:22:55
合計ジャッジ時間 17,504 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}
func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0
	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 2
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}
	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	solve(scanner, writer)
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)
	w := getNextInt(scanner)
	k := getNextInt64(scanner)
	aa := make([]int, n)
	for i := 0; i < n; i++ {
		aa[i] = getNextInt(scanner)
	}
	dp1 := make([]mint, w+1)
	dp1[0] = 1
	for i := 0; i < w; i++ {
		for j := 0; j < n && i+aa[j] < w+1; j++ {
			dp1[i+aa[j]].addAs(dp1[i])
		}
	}
	dp2 := make([]mint, w<<1+1)
	dp2[0] = 1
	for i := 0; i < w<<1; i++ {
		for j := 0; j < n && i+aa[j] < w<<1+1; j++ {
			if i+aa[j] == w {
				continue
			}
			dp2[i+aa[j]].addAs(dp2[i])
		}
	}
	ini := make([]mint, 2)
	ini[0] = dp1[w]
	ini[1] = 1
	mat := makeGrid(2, 2)
	mat[0][0] = dp1[w]
	mat[0][1] = 1
	mat[1][0] = dp2[w<<1]

	for i := 0; i < 60; i++ {
		if (k-1)>>uint(i)%2 == 1 {
			ini0 := ini[0].mul(mat[0][0]).add(ini[1].mul(mat[1][0]))
			ini1 := ini[0].mul(mat[0][1]).add(ini[1].mul(mat[1][1]))
			ini[0] = ini0
			ini[1] = ini1
		}
		mat00 := mat[0][0].mul(mat[0][0]).add(mat[0][1].mul(mat[1][0]))
		mat01 := mat[0][0].mul(mat[0][1]).add(mat[0][1].mul(mat[1][1]))
		mat10 := mat[1][0].mul(mat[0][0]).add(mat[1][1].mul(mat[1][0]))
		mat11 := mat[1][0].mul(mat[0][1]).add(mat[1][1].mul(mat[1][1]))
		mat[0][0] = mat00
		mat[0][1] = mat01
		mat[1][0] = mat10
		mat[1][1] = mat11
	}

	fmt.Fprintln(writer, ini[0])
}

func makeGrid(h, w int) [][]mint {
	index := make([][]mint, h, h)
	data := make([]mint, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

type mint int64

func (mt mint) mod() mint {
	m := mint(1e9 + 7)
	mt %= m
	if mt < 0 {
		return mt + m
	}
	return mt
}
func (mt mint) inv() mint {
	var m, y mint
	m.subAs(2)
	dbl := mt
	y = 1
	for i := 0; i < 31; i++ {
		if m%2 == 1 {
			y.mulAs(dbl)
		}
		m >>= 1
		dbl.mulAs(dbl)
	}
	return y
}
func (mt mint) add(x mint) mint {
	return (mt + x).mod()
}
func (mt mint) sub(x mint) mint {
	return (mt - x).mod()
}
func (mt mint) mul(x mint) mint {
	return (mt * x).mod()
}
func (mt mint) div(x mint) mint {
	return mt.mul(x.inv())
}
func (mt *mint) addAs(x mint) *mint {
	*mt = mt.add(x)
	return mt
}
func (mt *mint) subAs(x mint) *mint {
	*mt = mt.sub(x)
	return mt
}
func (mt *mint) mulAs(x mint) *mint {
	*mt = mt.mul(x)
	return mt
}
func (mt *mint) divAs(x mint) *mint {
	*mt = mt.div(x)
	return mt
}
0