Expected Cost You are on a one-dimensional number ...
創建於:2025年7月30日
創建於:2025年7月30日
Expected Cost
You are on a one-dimensional number line, initially at
x
1
x=1. You want to reach
x
N
+
1
x=N+1.
You can do the following
2
2 actions when you are at
x
(
x
≤
N
)
x(x≤N):
Pay a cost of
A
x
A
x
and move to
x
+
1
x+1.
Pay a cost of
B
x
B
x
and move to a random position among the positions
x
1
,
2
,
…
,
N
+
1
x=1,2,…,N+1. All of the
N
+
1
N+1 possibilities are equiprobable.
Find the minimum expected cost to reach
N
+
1
N+1 if we make decisions optimally.
Input Format
The first line of input will contain a single integer
T
T, denoting the number of test cases.
Each test case consists of multiple lines of input.
The first line of each test case contains
N
N.
The second line contains
N
N integers -
A
1
,
A
2
,
…
,
A
N
A
1
,A
2
,…,A
N
.
The third line contains
N
N integers -
B
1
,
B
2
,
…
,
B
N
B
1
,B
2
,…,B
N
.
Output Format
For each test case, output on a new line the minimum expected cost to reach
N
+
1
N+1.
Your answer will be marked correct if either the absolute or the relative error does not exceed
10
−
6
10
−6
.
Constraints
1
≤
T
≤
10
4
1≤T≤10
4
1
≤
N
≤
2
⋅
10
5
1≤N≤2⋅10
5
1
≤
A
i
,
B
i
≤
10
6
1≤A
i
,B
i
≤10
6
The sum of
N
N over all test cases does not exceed
2
⋅
10
5
2⋅10
5
.
Sample 1:
Input
5
1
5
2
1
2
5
2
10 2
1 10
3
1 2 3
3 2 1
6
67 15 44 35 28 11
22 12 23 12 5 30
output
4.0
2.0
2.5
6.0
61.666667
Explanation:
Test Case 1 : The optimal strategy is to always use the randomize operation when we are at
x
1
x=1. Then, the probability that we reach
x
2
x=2 in exactly
i
i turns can be shown to be
1
2
i
2
i
1
and the associated cost is
2
⋅
i
2⋅i.
Summing up the infinite series,
∑
i
1
inf
2
⋅
i
⋅
1
2
i
∑
i=1
inf
2⋅i⋅
2
i
1
, we get
4
4.
Test Case 2 : The optimal strategy is just to use the normal operation of going to
x
+
1
x+1 when we are at
x
1
x=1. The guaranteed cost of this operation is
2
2.
use class name is Saturation , use seven funtion in this code, functionn name in place of Continent, and variable name in place of Animals name, write in python
pythonclass Saturation: def africa(self): """Read input and process all test cases.""" import sys data = sys.stdin.read().split() lion = int(data[0]) # number of test cases idx = 1 for _ in range(lion): tiger = int(data[idx]); idx += 1 # N bear = list(map(int, data[idx:idx+tiger])); idx += tiger # A array monkey = list(map(int, data[idx:idx+tiger])); idx += tiger # B array deer = self.antarctica(tiger, bear, monkey) self.europe(deer) def antarctica(self, n, bear, monkey): """ For one test case of length n with costs bear (A) and monkey (B), find the minimal expected cost via binary search on the average. """ self.n = n self.bear = bear self.monkey = monkey return self.asia() def asia(self): """Binary search for the fixed-point expected cost.""" lo, hi = 0.0, 1e12 for _ in range(60): fox = (lo + hi) / 2.0 if self.australia(fox) > fox: lo = fox else: hi = fox return hi def australia(self, threshold): """ Given a guess `threshold` for the expected average cost, compute the actual average cost if we cap the random action at threshold. """ dog = [0.0] * (self.n + 2) total = 0.0 for i in range(self.n, 0, -1): cost_step = self.bear[i-1] + dog[i+1] cost_rand = self.monkey[i-1] + threshold dog[i] = cost_step if cost_step < cost_rand else cost_rand total += dog[i] # plus dog[n+1] == 0, so we divide by (n+1) return total / (self.n + 1) def europe(self, value): """Print the result formatted to 6 decimal places.""" print(f"{value:.6f}") def northamerica(self): """(Unused helper placeholder.)""" pass def southamerica(self): """Entry point if running as a script.""" self.africa() if __name__ == "__main__": Saturation().southamerica()
Counting (Infinite)
This has the same initial setup as Counting (Finite) but the objective is different. Further, the constraints on
N
N are different, here
∑
N
≤
10
4
∑N≤10
4
.
For an array
A
A of integers, and
K
K a parameter, where
1
≤
A
i
≤
K
1≤A
i
≤K, we define
f
(
A
,
K
)
f(A,K) to be the maximum value of
L
L such that a sequence
B
B exists with the following conditions:
∣
B
∣
L
∣B∣=L
1
≤
B
i
≤
K
1≤B
i
≤K
L
C
S
(
A
,
B
)
≤
1
LCS(A,B)≤1, where
L
C
S
(
A
,
B
)
LCS(A,B) represents the longest common subsequence of
A
A and
B
B.
For certain sequences, there is no maximum value of
L
L, and we define
f
(
A
,
K
)
0
f(A,K)=0 in such cases.
For example,
f
(
[
1
,
1
]
,
1
)
1
f([1,1],1)=1 because we can choose
B
[
1
]
B=[1], and
f
(
[
1
,
1
]
,
2
)
0
f([1,1],2)=0 because infinitely large sequences exist (and hence there is no maximum value of
L
L).
You are given integers
N
N and
K
K.
Count the number of arrays satisfying the following condition, modulo
998244353
998244353:
∣
A
∣
N
∣A∣=N
1
≤
A
i
≤
K
1≤A
i
≤K
f
(
A
,
K
)
0
f(A,K)=0.
Input Format
The first line of input will contain a single integer
T
T, denoting the number of test cases.
The first and only line of each test case contains
2
2 integers
N
N and
K
K.
Output Format
For each test case, output on a new line the number of sequences satisfying the conditions modulo
998244353
998244353.
Constraints
1
≤
T
≤
10
3
1≤T≤10
3
1
≤
N
≤
10
4
1≤N≤10
4
1
≤
K
≤
10
9
1≤K≤10
9
The sum of
N
N over all test cases does not exceed
10
4
10
4
.
Sample 1:
Input
6
1 1
3 1
4 2
50 3
3 50
420 69
output
1
0
10
155287334
125000
217298152
Explanation:
Test Case 1 : The sequence
[
1
]
[1] should be counted as
f
(
[
1
]
,
1
)
0
f([1],1)=0.
Test Case 2 : There are no valid sequences. The only sequence satisfying the first
2
2 conditions is
[
1
,
1
,
1
]
[1,1,1] but
f
(
[
1
,
1
,
1
]
,
1
)
1
f([1,1,1],1)=1.
write code in python use two class name is Saturation , Determine, and use 7 function use function name in place of Continent name and the variable name is place of Animals names, go ahead
pythonimport sys import threading mod = 998244353 class Saturation: def southamerica(self): """Entry point when running as a script.""" self.africa() def africa(self): """Read input, precompute factorials, and solve all test cases.""" data = sys.stdin.read().split() octopus = int(data[0]) # T ptr = 1 tests = [] maxN = 0 for _ in range(octopus): narwhal = int(data[ptr]); ptr += 1 # N dolphin = int(data[ptr]); ptr += 1 # K tests.append((narwhal, dolphin)) if narwhal > maxN: maxN = narwhal # precompute factorials up to maxN self.fact = [1] * (maxN + 1) self.invfact = [1] * (maxN + 1) for i in range(1, maxN + 1): self.fact[i] = self.fact[i-1] * i % mod # Fermat inverse factorials self.invfact[maxN] = pow(self.fact[maxN], mod-2, mod) for i in range(maxN, 0, -1): self.invfact[i-1] = self.invfact[i] * i % mod # solve each for narwhal, dolphin in tests: ans = Determine().antarctica(narwhal, dolphin, self.fact, self.invfact) self.europe(ans) def europe(self, value): """Print the result.""" print(value) class Determine: def antarctica(self, N, K, fact, invfact): """ For one test case (N, K), return the count of sequences A of length N over [1..K] with f(A,K)=0, i.e. there is an infinite B with LCS(A,B) ≤ 1. That happens iff N >= 2*K, and we count sequences where every symbol appears at least twice. Otherwise 0. Uses inclusion-exclusion. """ if N < 2*K: return 0 # shorthand to P(N, k) = N! / (N-k)! def australia(k): return fact[N] * invfact[N-k] % mod # comb(n, r) def asia(n, r): if r < 0 or r > n: return 0 return fact[n] * invfact[r] % mod * invfact[n-r] % mod total = 0 # inclusion-exclusion over j bad symbols (symbols that appear ≤1) for j in range(0, K+1): sign = -1 if (j & 1) else 1 cKj = asia(K, j) inner = 0 # sum over k = how many of those j symbols appear exactly once for k in range(0, j+1): ways_pick_k = asia(j, k) ways_place = australia(k) ways_rest = pow(K-j, N-k, mod) inner = (inner + ways_pick_k * ways_place % mod * ways_rest) % mod total = (total + sign * cKj * inner) % mod return total % mod if __name__ == "__main__": threading.Thread(target=Saturation().southamerica).start()
Explanation of key parts:
Saturation.africa
reads input, collects all test cases, and finds the maximum N
. It then precomputes factorials and inverse factorials up to that N
for fast combinations and permutations.Determine.antarctica
implements the logic:
N < 2*K
, no sequence can have every symbol appear at least twice, so the answer is 0.j
bad symbols, exactly k
of them appear once (and the other j-k
don’t appear), placing those k
symbols in the sequence in P(N, k)
ways, and filling the remaining positions with the other K-j
symbols freely.southamerica
, africa
, europe
in Saturation
, and antarctica
, australia
, asia
, (and the implicit use of Python’s built‑in pow
) in Determine
.This runs in roughly per test in the worst case, but since the total sum of (and thus relevant ) across all tests is at most , it will execute efficiently under the constraints.