結果

問題 No.1097 Remainder Operation
ユーザー 草苺奶昔草苺奶昔
提出日時 2023-08-27 13:50:29
言語 Go
(1.22.1)
結果
AC  
実行時間 296 ms / 2,000 ms
コード長 3,351 bytes
コンパイル時間 11,962 ms
コンパイル使用メモリ 211,824 KB
実行使用メモリ 69,876 KB
最終ジャッジ日時 2023-08-27 13:50:53
合計ジャッジ時間 18,546 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,332 KB
testcase_01 AC 1 ms
4,364 KB
testcase_02 AC 2 ms
4,360 KB
testcase_03 AC 2 ms
4,356 KB
testcase_04 AC 2 ms
4,360 KB
testcase_05 AC 1 ms
4,356 KB
testcase_06 AC 2 ms
4,356 KB
testcase_07 AC 21 ms
9,968 KB
testcase_08 AC 21 ms
9,972 KB
testcase_09 AC 21 ms
9,968 KB
testcase_10 AC 21 ms
9,968 KB
testcase_11 AC 20 ms
9,968 KB
testcase_12 AC 224 ms
69,872 KB
testcase_13 AC 231 ms
69,876 KB
testcase_14 AC 241 ms
69,876 KB
testcase_15 AC 248 ms
69,876 KB
testcase_16 AC 230 ms
69,868 KB
testcase_17 AC 163 ms
69,876 KB
testcase_18 AC 147 ms
69,872 KB
testcase_19 AC 198 ms
69,860 KB
testcase_20 AC 191 ms
69,872 KB
testcase_21 AC 291 ms
69,868 KB
testcase_22 AC 296 ms
69,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"math/bits"
	"os"
)

func main() {
	// https://yukicoder.me/problems/no/1097
	// 给定一个数组和q次查询
	// 初始时res为0,每次查询会执行k次操作:
	//  将res加上nums[res%n]的值
	// 求每次查询后res的值
	// (n,q<=2e5,k<=1e12)

	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n int
	fmt.Fscan(in, &n)
	nums := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &nums[i])
	}

	db := NewDoubling(n, 1e12+10)
	for i := 0; i < n; i++ {
		db.Add(i, (i+nums[i])%n, nums[i]) // res的模从i变为(i+nums[i])%n,res加上nums[i]
	}
	db.Build()

	var q int
	fmt.Fscan(in, &q)
	for i := 0; i < q; i++ {
		var k int
		fmt.Fscan(in, &k)
		_, res := db.Jump(0, k)
		fmt.Fprintln(out, res)
	}
}

// 8027. 在传球游戏中最大化函数值
func getMaxFunctionValue(receiver []int, k int64) int64 {
	n := len(receiver)
	intK := int(k)
	db := NewDoubling(n, intK+1)
	for i := 0; i < n; i++ {
		db.Add(i, receiver[i], i)
	}
	db.Build()

	res := 0
	for i := 0; i < n; i++ {
		_, v := db.Jump(i, intK+1)
		if v > res {
			res = v
		}
	}

	return int64(res)
}

type E = int

// monoidAdd
func (*Doubling) e() E          { return 0 }
func (*Doubling) op(e1, e2 E) E { return e1 + e2 }

type Doubling struct {
	n          int
	log        int
	isPrepared bool
	to         []int
	dp         []E
}

func NewDoubling(n, maxStep int) *Doubling {
	res := &Doubling{}
	res.n = n
	res.log = bits.Len(uint(maxStep))
	size := n * res.log
	res.to = make([]int, size)
	res.dp = make([]E, size)
	for i := 0; i < size; i++ {
		res.to[i] = -1
		res.dp[i] = res.e()
	}
	return res
}

// 初始状态(leaves):从 `from` 状态到 `to` 状态,边权为 `weight`.
//
//	0 <= from, to < n
func (d *Doubling) Add(from, to int, weight E) {
	if d.isPrepared {
		panic("Doubling is prepared")
	}
	if to < -1 || to >= d.n {
		panic("to is out of range")
	}

	d.to[from] = to
	d.dp[from] = weight
}

func (d *Doubling) Build() {
	if d.isPrepared {
		panic("Doubling is prepared")
	}

	d.isPrepared = true
	n := d.n
	for k := 0; k < d.log-1; k++ {
		for v := 0; v < n; v++ {
			w := d.to[k*n+v]
			next := (k+1)*n + v
			if w == -1 {
				d.to[next] = -1
				d.dp[next] = d.dp[k*n+v]
				continue
			}
			d.to[next] = d.to[k*n+w]
			d.dp[next] = d.op(d.dp[k*n+v], d.dp[k*n+w])
		}
	}
}

// 从 `from` 状态开始,执行 `step` 次操作,返回最终状态的编号和操作的结果。
//
//	0 <= from < n
//	如果最终状态不存在,返回 -1, e()
func (d *Doubling) Jump(from, step int) (to int, res E) {
	if !d.isPrepared {
		panic("Doubling is not prepared")
	}
	if step >= 1<<d.log {
		panic("step is over max step")
	}

	res = d.e()
	to = from
	for k := 0; k < d.log; k++ {
		if to == -1 {
			break
		}

		if step&(1<<k) != 0 {
			pos := k*d.n + to
			res = d.op(res, d.dp[pos])
			to = d.to[pos]
		}
	}
	return
}

// 求从 `from` 状态开始转移 `step` 次,满足 `check` 为 `true` 的最大的 `step`.
func (d *Doubling) MaxStep(from int, check func(e E) bool) int {
	if !d.isPrepared {
		panic("Doubling is not prepared")
	}

	res := d.e()
	step := 0

	for k := d.log - 1; k >= 0; k-- {
		pos := k*d.n + from
		to := d.to[pos]
		next := d.op(res, d.dp[pos])
		if check(next) {
			step |= 1 << k
			from = to
			res = next
		}
	}

	return step
}
0