結果
| 問題 |
No.1084 積の積
|
| コンテスト | |
| ユーザー |
sgsw
|
| 提出日時 | 2021-08-14 14:00:11 |
| 言語 | Go (1.23.4) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,158 bytes |
| コンパイル時間 | 12,481 ms |
| コンパイル使用メモリ | 227,340 KB |
| 実行使用メモリ | 7,504 KB |
| 最終ジャッジ日時 | 2024-10-05 07:13:11 |
| 合計ジャッジ時間 | 12,002 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 26 WA * 1 |
ソースコード
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)
}
sgsw