Rufo's massive#12430

Rufo's massive (Editorial)

Problem Author:

AzeTurk810

Prepared by:

AzeTurk810

Edited by:

Hasan Saleh (MathModel)

Solution

The transformation rule dictates that for any pair (a_i, b_i), must satisfy either b_i = x or b_i = a_i(x-1). By focusing on the first element (a_1, b_1), we identify at most two candidate values for the secret integer: x = b_1 or x = \frac{b_1}{a_1} + 1 (provided b_1 is a multiple of a_1 and x \ge 1). Since must be consistent for the entire array, we verify if either candidate correctly produces every b_i from a_i using the given logic: if a_i > x and a_i(x-1) > x, then b_i = a_i(x-1); otherwise, b_i = x. If a candidate satisfies this for all indices, it is a valid answer; if neither candidate works, the result is .

def ok(x,n,a,b):
	if x<1: return 0
	for i in range(n):
		if a[i]>x and a[i]*(x-1)>x: r=a[i]*(x-1)
		else: r=x
		if r!=b[i]: return 0
	return 1

def solve():
	n=int(input())
	a=list(map(int,input().split()))
	b=list(map(int,input().split()))
	if ok(b[0],n,a,b):
		print(b[0]); return
	if b[0]%a[0]==0:
		x=b[0]//a[0]+1
		if ok(x,n,a,b):
			print(x); return
	print(-1)

t=int(input())
for _ in range(t):
	solve()