結果

問題 No.118 門松列(2)
ユーザー yukirinyukirin
提出日時 2016-03-02 02:40:01
言語 Go
(1.22.1)
結果
AC  
実行時間 20 ms / 5,000 ms
コード長 1,030 bytes
コンパイル時間 12,937 ms
コンパイル使用メモリ 230,188 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-19 06:32:21
合計ジャッジ時間 12,590 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

package main

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

var sc = bufio.NewScanner(os.Stdin)

func main() {
	sc.Split(bufio.ScanWords)
	n := nextInt()
	bamboos := make([]int, 0, n)
	m := make(map[int]int64)
	c, mod := int64(0), int64(1e9+7)

	for i := 0; i < n; i++ {
		m[nextInt()]++
	}

	for k := range m {
		bamboos = append(bamboos, k)
	}

	comb(bamboos, 3, func(a []int) {
		if isKD(a) {
			c += m[a[0]] * m[a[1]] * m[a[2]] % mod
		}
	})

	fmt.Println(c % mod)
}

func isKD(a []int) bool {
	if a[0] == a[1] || a[1] == a[2] || a[0] == a[2] {
		return false
	}
	return true
}

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

func nextInt() int {
	i, _ := strconv.Atoi(nextLine())
	return i
}

func comb(a []int, r int, fn func([]int)) {
	if r > len(a) || r < 1 {
		return
	}

	var f func([]int, int, int)
	f = func(ret []int, last, c int) {
		if c == 0 {
			fn(ret)
			return
		}

		c--
		for i, v := range a[last : len(a)-c] {
			ret[r-c-1] = v
			f(ret, i+last+1, c)
		}
	}

	ret := make([]int, r)
	f(ret, 0, r)
}
0