結果

問題 No.389 ロジックパズルの組み合わせ
ユーザー aru aruaru aru
提出日時 2020-10-18 20:49:34
言語 Go
(1.22.1)
結果
AC  
実行時間 233 ms / 2,000 ms
コード長 2,609 bytes
コンパイル時間 14,246 ms
コンパイル使用メモリ 211,828 KB
実行使用メモリ 32,844 KB
最終ジャッジ日時 2023-09-28 12:04:40
合計ジャッジ時間 21,217 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 232 ms
27,808 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 233 ms
30,748 KB
testcase_10 AC 24 ms
7,800 KB
testcase_11 AC 229 ms
32,844 KB
testcase_12 AC 11 ms
5,520 KB
testcase_13 AC 74 ms
14,232 KB
testcase_14 AC 24 ms
7,872 KB
testcase_15 AC 34 ms
7,956 KB
testcase_16 AC 103 ms
15,432 KB
testcase_17 AC 150 ms
22,700 KB
testcase_18 AC 142 ms
20,492 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 1 ms
4,376 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 1 ms
4,380 KB
testcase_41 AC 1 ms
4,380 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 1 ms
4,380 KB
testcase_44 AC 1 ms
4,376 KB
testcase_45 AC 1 ms
4,380 KB
testcase_46 AC 2 ms
4,376 KB
testcase_47 AC 1 ms
4,380 KB
testcase_48 AC 1 ms
4,376 KB
testcase_49 AC 29 ms
20,388 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 2 ms
4,376 KB
testcase_52 AC 3 ms
5,520 KB
testcase_53 AC 3 ms
5,488 KB
testcase_54 AC 5 ms
7,800 KB
testcase_55 AC 1 ms
4,380 KB
testcase_56 AC 3 ms
5,488 KB
testcase_57 AC 9 ms
11,992 KB
testcase_58 AC 21 ms
20,368 KB
testcase_59 AC 20 ms
16,208 KB
testcase_60 AC 7 ms
9,880 KB
testcase_61 AC 33 ms
26,744 KB
testcase_62 AC 8 ms
9,880 KB
testcase_63 AC 27 ms
22,460 KB
testcase_64 AC 6 ms
7,800 KB
testcase_65 AC 3 ms
5,504 KB
testcase_66 AC 6 ms
7,816 KB
testcase_67 AC 6 ms
7,804 KB
testcase_68 AC 6 ms
7,800 KB
testcase_69 AC 125 ms
18,500 KB
testcase_70 AC 58 ms
10,512 KB
testcase_71 AC 91 ms
14,216 KB
testcase_72 AC 38 ms
8,012 KB
testcase_73 AC 71 ms
12,296 KB
testcase_74 AC 99 ms
16,356 KB
testcase_75 AC 85 ms
14,220 KB
testcase_76 AC 31 ms
7,800 KB
testcase_77 AC 171 ms
22,688 KB
testcase_78 AC 10 ms
5,492 KB
testcase_79 AC 2 ms
4,380 KB
testcase_80 AC 2 ms
4,376 KB
testcase_81 AC 1 ms
4,380 KB
testcase_82 AC 1 ms
4,380 KB
testcase_83 AC 1 ms
4,376 KB
testcase_84 AC 1 ms
4,376 KB
testcase_85 AC 1 ms
4,376 KB
testcase_86 AC 1 ms
4,376 KB
testcase_87 AC 2 ms
4,376 KB
testcase_88 AC 1 ms
4,376 KB
testcase_89 AC 45 ms
10,056 KB
testcase_90 AC 80 ms
14,392 KB
testcase_91 AC 143 ms
20,496 KB
testcase_92 AC 19 ms
7,800 KB
testcase_93 AC 86 ms
16,388 KB
testcase_94 AC 2 ms
4,376 KB
testcase_95 AC 91 ms
14,380 KB
testcase_96 AC 71 ms
12,264 KB
testcase_97 AC 67 ms
12,268 KB
testcase_98 AC 29 ms
7,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

var sc = bufio.NewScanner(os.Stdin)
var wr = bufio.NewWriter(os.Stdout)

func out(x ...interface{}) {
	fmt.Fprintln(wr, x...)
}

func getI() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func getF() float64 {
	sc.Scan()
	i, e := strconv.ParseFloat(sc.Text(), 64)
	if e != nil {
		panic(e)
	}
	return i
}

func getInts(N int) []int {
	ret := make([]int, N)
	for i := 0; i < N; i++ {
		ret[i] = getI()
	}
	return ret
}

func getS() string {
	sc.Scan()
	return sc.Text()
}

// min, max, asub, absなど基本関数
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func asub(a, b int) int {
	if a > b {
		return a - b
	}
	return b - a
}

func abs(a int) int {
	if a >= 0 {
		return a
	}
	return -a
}

func lowerBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}

func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanLines)
	sc.Buffer([]byte{}, 1000000)
	// this template is new version.
	// use getI(), getS(), getInts(), getF()
	M := getI()
	s := strings.Split(getS(), " ")
	h := make([]int, 0)
	tot := 0
	for _, e := range s {
		v, _ := strconv.Atoi(e)
		h = append(h, v)
		tot += v
	}
	if tot == 0 {
		out(1)
		return
	}
	tot += len(h) - 1
	m := M - tot
	n := len(h)

	if m < 0 {
		out("NA")
		return
	}

	out(nCr(n+m, m))
}

const mod = 1000000007

func mpow(a, b int) int {
	if b == 0 {
		return 1
	} else if b%2 == 0 {
		tmp := mpow(a, b/2)
		return tmp * tmp % mod
	}
	return mpow(a, b-1) * a % mod
}

var fracMemo = []int{1, 1}

func mfrac(n int) int {
	if len(fracMemo) > n {
		return fracMemo[n]
	}
	if len(fracMemo) == 0 {
		fracMemo = append(fracMemo, 1)
	}
	for len(fracMemo) <= n {
		size := len(fracMemo)
		fracMemo = append(fracMemo, fracMemo[size-1]*size%mod)
	}
	return fracMemo[n]
}

var ifracMemo = []int{1, 1}

func mifrac(n int) int {
	if len(ifracMemo) > n {
		return ifracMemo[n]
	}
	if len(ifracMemo) == 0 {
		fracMemo = append(ifracMemo, 1)
	}
	for len(ifracMemo) <= n {
		size := len(ifracMemo)
		ifracMemo = append(ifracMemo, ifracMemo[size-1]*mpow(size, mod-2)%mod)
	}
	return ifracMemo[n]
}

func nCr(n, r int) int {
	if n == r {
		return 1
	}
	if n < r || r < 0 {
		return 0
	}
	ret := 1
	ret *= mfrac(n)
	ret %= mod
	ret *= mifrac(r)
	ret %= mod
	ret *= mifrac(n - r)
	ret %= mod
	return (ret)
}
0