結果

問題 No.368 LCM of K-products
ユーザー sgswsgsw
提出日時 2021-08-17 22:31:09
言語 Go
(1.22.1)
結果
AC  
実行時間 379 ms / 2,000 ms
コード長 1,670 bytes
コンパイル時間 11,966 ms
コンパイル使用メモリ 232,564 KB
実行使用メモリ 5,888 KB
最終ジャッジ日時 2024-04-19 06:03:47
合計ジャッジ時間 15,513 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 189 ms
5,248 KB
testcase_01 AC 156 ms
5,888 KB
testcase_02 AC 263 ms
5,376 KB
testcase_03 AC 195 ms
5,376 KB
testcase_04 AC 178 ms
5,376 KB
testcase_05 AC 379 ms
5,504 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 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 136 ms
5,376 KB
testcase_14 AC 213 ms
5,376 KB
testcase_15 AC 232 ms
5,632 KB
testcase_16 AC 205 ms
5,376 KB
testcase_17 AC 170 ms
5,376 KB
testcase_18 AC 249 ms
5,632 KB
testcase_19 AC 58 ms
5,376 KB
testcase_20 AC 154 ms
5,376 KB
testcase_21 AC 37 ms
5,376 KB
testcase_22 AC 237 ms
5,632 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
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 2 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
testcase_33 AC 90 ms
5,376 KB
testcase_34 AC 7 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

var wg = bufio.NewScanner(os.Stdin)

const (
	inf            = int(1e18)
	initialBufSize = int(1e6)
	maxBufSize     = int(1e6)
	mod            = int(1e9 + 7)
)

var buf []byte = make([]byte, initialBufSize)

func factorize(n int) map[int]int {
	div := n
	retval := make(map[int]int)
	for i := 2; i*i <= n; i++ {
		if div%i == 0 {
			ord := 0
			for div%i == 0 {
				div /= i
				ord++
			}
			retval[i] = ord
		}
	}
	if div > 1 {
		retval[div] = 1
	}
	return retval
}

func main() {
	n, k := nextInt(), nextInt()
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = nextInt()
	}
	fact_a := make([]map[int]int, n)
	prime_set := make(map[int]bool)
	for i := 0; i < n; i++ {
		fact_a[i] = factorize(a[i])
		for key := range fact_a[i] {
			prime_set[key] = true
		}
	}
	ans := 1
	for p := range prime_set {
		b := make([]int, n)
		for i := 0; i < n; i++ {
			if val, ok := fact_a[i][p]; ok {
				b[i] = val
			}
		}
		sort.Slice(b, func(i, j int) bool {
			return b[i] > b[j]
		})
		ord := 0
		for i := 0; i < k; i++ {
			ord += b[i]
		}
		ans = ans * powMod(p, ord, mod) % mod
	}
	fmt.Printf("%d\n", ans)
}

// greatest common divisor (GCD) via Euclidean algorithm
func GCD(a, b int) int {
	for b != 0 {
		t := b
		b = a % b
		a = t
	}
	return a
}

func powMod(x, n, mod int) int {
	retval := 1
	for mul := x; n > 0; {
		if n&1 == 1 {
			retval = retval * mul % mod
		}
		mul = mul * mul % mod
		n >>= 1
	}
	return retval
}

func init() {
	wg.Split(bufio.ScanWords)
	wg.Buffer(buf, maxBufSize)
}

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