結果

問題 No.1084 積の積
ユーザー sgswsgsw
提出日時 2021-08-14 14:00:11
言語 Go
(1.22.1)
結果
WA  
実行時間 -
コード長 2,158 bytes
コンパイル時間 13,786 ms
コンパイル使用メモリ 224,016 KB
実行使用メモリ 7,504 KB
最終ジャッジ日時 2024-04-15 11:47:51
合計ジャッジ時間 16,396 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 WA -
testcase_05 AC 6 ms
6,940 KB
testcase_06 AC 28 ms
7,500 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 12 ms
6,940 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 4 ms
6,940 KB
testcase_12 AC 38 ms
6,944 KB
testcase_13 AC 78 ms
7,500 KB
testcase_14 AC 29 ms
6,944 KB
testcase_15 AC 27 ms
6,940 KB
testcase_16 AC 89 ms
7,500 KB
testcase_17 AC 35 ms
6,940 KB
testcase_18 AC 58 ms
6,944 KB
testcase_19 AC 78 ms
7,500 KB
testcase_20 AC 20 ms
6,940 KB
testcase_21 AC 30 ms
6,940 KB
testcase_22 AC 23 ms
6,944 KB
testcase_23 AC 47 ms
6,944 KB
testcase_24 AC 43 ms
6,944 KB
testcase_25 AC 78 ms
7,504 KB
testcase_26 AC 86 ms
7,500 KB
testcase_27 AC 86 ms
7,500 KB
testcase_28 AC 86 ms
7,500 KB
testcase_29 AC 87 ms
7,500 KB
testcase_30 AC 86 ms
7,500 KB
testcase_31 AC 86 ms
7,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

var wg = bufio.NewScanner(os.Stdin)

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

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

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

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func modInv(a, modulo int) int {
	b := modulo
	u, v := 1, 0
	for b > 0 {
		t := a / b
		a, b = b, a-t*b
		u, v = v, u-t*v
	}
	u %= modulo
	if u < 0 {
		u += modulo
	}
	return u
}

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 nextInt() int {
	wg.Scan()
	i, e := strconv.Atoi(wg.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func main() {
	n := nextInt()
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = nextInt()
	}
	for _, v := range a {
		if v == 0 {
			fmt.Printf("%d\n", 0)
			return
		}
	}
	itrs := make([]int, n)
	for product, itr, i := 1, 0, 0; i < n; i++ {
		itr = max(itr, i)
		for itr < n && product*a[itr] <= maxnum {
			product *= a[itr]
			itr++
		}
		itrs[i] = itr
		if itr != i {
			product /= a[i]
		}
	}

	product_a := make([]int, n)
	product_product_a := make([]int, n)

	for i := 0; i < n; i++ {
		if i > 0 {
			product_a[i] = product_a[i-1] * a[i] % mod
		} else {
			product_a[i] = a[i] % mod
		}
	}

	for i := 0; i < n; i++ {
		if i > 0 {
			product_product_a[i] = product_product_a[i-1] * product_a[i] % mod
		} else {
			product_product_a[i] = product_a[i] % mod
		}
	}

	final_ans := 1

	subtask := func(l, r int) int {
		if l > 0 {
			retval := product_product_a[r-1] * modInv(product_product_a[l-1], mod) % mod
			retval = retval * modInv(powMod(product_a[l-1], r-l, mod), mod) % mod
			return retval
		} else {
			retval := product_product_a[r-1] % mod
			return retval
		}
	}
	for l := 0; l < n; l++ {
		if itrs[l] == l {
			continue
		}
		//itrs[l] > l
		final_ans = final_ans * subtask(l, itrs[l]) % mod
	}
	fmt.Printf("%d\n", final_ans)
}
0