Aroma’s Search
With a new body, our idol Aroma White (or should we call her Kaori Minamiya?) begins to uncover her lost past through the OS space.
The space can be considered a 2D plane, with an infinite number of data nodes, indexed from 0 0 0, with their coordinates defined as follows:
- The coordinates of the 0 0 0-th node is ( x 0 , y 0 ) (x_0, y_0) (x0,y0)
- For i > 0 i > 0 i>0, the coordinates of i i i-th node is ( a x ⋅ x i − 1 + b x , a y ⋅ y i − 1 + b y ) (a_x \cdot x_{i-1} + b_x, a_y \cdot y_{i-1} + b_y) (ax⋅xi−1+bx,ay⋅yi−1+by)
Initially Aroma stands at the point ( x s , y s ) (x_s, y_s) (xs,ys). She can stay in OS space for at most t t t seconds, because after this time she has to warp back to the real world. She doesn’t need to return to the entry point ( x s , y s ) (x_s, y_s) (xs,ys) to warp home.
While within the OS space, Aroma can do the following actions:
- From the point ( x , y ) (x, y) (x,y), Aroma can move to one of the following points: ( x − 1 , y ) (x-1, y) (x−1,y), ( x + 1 , y ) (x+1, y) (x+1,y), ( x , y − 1 ) (x, y-1) (x,y−1) or ( x , y + 1 ) (x, y+1) (x,y+1). This action requires 1 1 1 second.
- If there is a data node at where Aroma is staying, she can collect it. We can assume this action costs 0 0 0 seconds. Of course, each data node can be collected at most once.
Aroma wants to collect as many data as possible before warping back. Can you help her in calculating the maximum number of data nodes she could collect within t t t seconds?
Input
The first line contains integers x 0 x_0 x0, y 0 y_0 y0, a x a_x ax, a y a_y ay, b x b_x bx, b y b_y by ( 1 ≤ x 0 , y 0 ≤ 1 0 16 1 \leq x_0, y_0 \leq 10^{16} 1≤x0,y0≤1016, 2 ≤ a x , a y ≤ 100 2 \leq a_x, a_y \leq 100 2≤ax,ay≤100, 0 ≤ b x , b y ≤ 1 0 16 0 \leq b_x, b_y \leq 10^{16} 0≤bx,by≤1016), which define the coordinates of the data nodes.
The second line contains integers x s x_s xs, y s y_s ys, t t t ( 1 ≤ x s , y s , t ≤ 1 0 16 1 \leq x_s, y_s, t \leq 10^{16} 1≤xs,ys,t≤1016) – the initial Aroma’s coordinates and the amount of time available.
Output
Print a single integer — the maximum number of data nodes Aroma can collect within t t t seconds.
Example
i n p u t \tt input input |
---|
1 1 2 3 1 0 2 4 20 |
o u t p u t \tt output output |
3 |
i n p u t \tt input input |
---|
1 1 2 3 1 0 15 27 26 |
o u t p u t \tt output output |
2 |
i n p u t \tt input input |
---|
1 1 2 3 1 0 2 2 1 |
o u t p u t \tt output output |
0 |
Note
In all three examples, the coordinates of the first 5 5 5 data nodes are ( 1 , 1 ) (1, 1) (1,1), ( 3 , 3 ) (3, 3) (3,3), ( 7 , 9 ) (7, 9) (7,9), ( 15 , 27 ) (15, 27) (15,27) and ( 31 , 81 ) (31, 81) (31,81) (remember that nodes are numbered from 0 0 0).
In the first example, the optimal route to collect 3 3 3 nodes is as follows:
- Go to the coordinates ( 3 , 3 ) (3, 3) (3,3) and collect the 1 1 1-st node. This takes ∣ 3 − 2 ∣ + ∣ 3 − 4 ∣ = 2 |3 - 2| + |3 - 4| = 2 ∣3−2∣+∣3−4∣=2 seconds.
- Go to the coordinates ( 1 , 1 ) (1, 1) (1,1) and collect the 0 0 0-th node. This takes ∣ 1 − 3 ∣ + ∣ 1 − 3 ∣ = 4 |1 - 3| + |1 - 3| = 4 ∣1−3∣+∣1−3∣=4 seconds.
- Go to the coordinates ( 7 , 9 ) (7, 9) (7,9) and collect the 2 2 2-nd node. This takes ∣ 7 − 1 ∣ + ∣ 9 − 1 ∣ = 14 |7 - 1| + |9 - 1| = 14 ∣7−1∣+∣9−1∣=14 seconds.
In the second example, the optimal route to collect 2 2 2 nodes is as follows:
- Collect the 3 3 3-rd node. This requires no seconds.
- Go to the coordinates ( 7 , 9 ) (7, 9) (7,9) and collect the 2 2 2-th node. This takes ∣ 15 − 7 ∣ + ∣ 27 − 9 ∣ = 26 |15 - 7| + |27 - 9| = 26 ∣15−7∣+∣27−9∣=26 seconds.
In the third example, Aroma can’t collect any nodes. She should have taken proper rest instead of rushing into the OS space like that.
Tutorial
可以根据 x 0 x_0 x0, y 0 y_0 y0, a x a_x ax, a y a_y ay, b x b_x bx, b y b_y by,将所有坐标绝对值小于 3 × 1 0 16 3 \times 10 ^ {16} 3×1016 的点全找出来,最多有 log 2 ( 3 × 1 0 16 ) \log_2 (3 \times 10 ^{16}) log2(3×1016) 个点
根据题意易得,如果将所有点依次连接起来,是一条具有递增趋势的折线,设 d ( i , j ) d(i, j) d(i,j) 为 i i i 节点和 j j j 节点的距离,对于所有的 1 ≤ u < v < w 1 \leq u < v < w 1≤u<v<w,有 d ( u , v ) + d ( v , w ) = d ( u , w ) d(u, v) + d(v, w) = d(u, w) d(u,v)+d(v,w)=d(u,w)
可以用三层循环,其中一个点为切入此折线的点,在该点左侧和右侧各找一个点,如果可以将这两个点范围内的点都跑完则更新距离,以此方法进行三层循环暴力找点
此解法时间复杂度为 O ( log 3 ( 3 × 1 0 16 ) ) \mathcal O(\log ^ 3 (3 \times 10 ^{16})) O(log3(3×1016)) , 100 m s 100\ ms 100 ms 内可以跑完
Solution
x0, y0, ax, ay, bx, by = map(int, input().split())
xs, ys, t = map(int, input().split())
points = [[x0, y0]]
while points[-1][0] <= 3 * 10 ** 16 and points[-1][1] <= 3 * 10 ** 16:
points.append([points[-1][0] * ax + bx, points[-1][1] * ay + by])
ans = 0
for i, [in_x, in_y] in enumerate(points):
now = t - abs(in_x - xs) - abs(in_y - ys)
for j, [left_x, left_y] in enumerate(points[: i + 1]):
for k, [right_x, right_y] in enumerate(points[i:]):
if now - (right_x - left_x + right_y - left_y) - min((in_x - left_x + in_y - left_y), (right_x - in_x + right_y - in_y)) >= 0:
ans = max(ans, k + (i - j) + 1)
print(ans)