CF2145F.Long Journey

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There is a strip divided into cells, numbered from 00 to mm from left to right. You are controlling a chip that is initially in the cell 00.

There is a trap in each cell; they are activated according to the following rules:

  • at the end of moves 1,(1+n),(1+2n),…1, (1+n), (1+2n), \dots, traps are activated in cells xx where x mod a1=b1x \bmod a_1 = b_1;
  • at the end of moves 2,(2+n),(2+2n),…2, (2+n), (2+2n), \dots, traps are activated in cells xx where x mod a2=b2x \bmod a_2 = b_2;
  • ⋯\cdots
  • at the end of moves n,(n+n),(n+2n),…n, (n+n), (n+2n), \dots, traps are activated in cells xx where x mod an=bnx \bmod a_n= b_n.

In one turn, you can either move from the current cell to the next or stay in place. Then all the traps for this turn are activated. If the chip is in a cell with an activated trap at the beginning of the turn, the game ends.

Your task is to calculate the minimum number of turns to reach the cell mm, or report that it is impossible. If the chip reaches the cell mm and at the end of the same turn, a trap in the cell mm activates, it is not considered a valid way to reach the cell mm.

有一条被划分为若干格子的长条,格子从左到右编号为 00 到 mm。你控制一个初始位于格子 00 的芯片。

每个格子中均设有一个陷阱,其激活规则如下:

  • 在第 1, (1+n), (1+2n), …1,\ (1+n),\ (1+2n),\ \dots 步结束时,所有满足 x mod a1=b1x \bmod a_1 = b_1 的格子 xx 中的陷阱被激活;
  • 在第 2, (2+n), (2+2n), …2,\ (2+n),\ (2+2n),\ \dots 步结束时,所有满足 x mod a2=b2x \bmod a_2 = b_2 的格子 xx 中的陷阱被激活;
  • ⋯\cdots
  • 在第 n, (n+n), (n+2n), …n,\ (n+n),\ (n+2n),\ \dots 步结束时,所有满足 x mod an=bnx \bmod a_n = b_n 的格子 xx 中的陷阱被激活。

每一轮中,你可以选择将芯片从当前格子移动至右侧相邻格子,或保持不动。随后,该轮对应的所有陷阱将被激活。若芯片在本轮开始时所处格子中存在已被激活的陷阱,则游戏立即结束。

你的任务是计算到达格子 mm 所需的最少轮数;若无法到达,则报告不可能。特别地,若芯片在某轮中抵达格子 mm,但该轮结束时格子 mm 的陷阱恰好被激活,则此路径不被视为有效到达格子 mm 的方式。

输入格式

The first line contains a single integer tt (1≤t≤1001 \le t \le 100) — the number of test cases.

The first line of each test case contains two integers nn and mm (1≤n≤101 \le n \le 10; 1≤m≤10121 \le m \le 10^{12}).

The second line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (2≤ai≤102 \le a_i \le 10).

The third line contains nn integers b1,b2,…,bnb_1, b_2, \dots, b_n (0≤bi<ai0 \le b_i \lt a_i).

第一行包含一个整数 tt(1≤t≤1001 \le t \le 100)—— 表示测试用例的数量。

每个测试用例的第一行包含两个整数 nn 和 mm(1≤n≤101 \le n \le 10;1≤m≤10121 \le m \le 10^{12})。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(2≤ai≤102 \le a_i \le 10)。

第三行包含 nn 个整数 b1,b2,…,bnb_1, b_2, \dots, b_n(0≤bi<ai0 \le b_i \lt a_i)。

输出格式

For each test case, print a single integer — the minimum number of turns to reach cell mm. If it is impossible, print -1.

对于每个测试用例,输出一个整数——到达单元格 mm 所需的最少步数。如果无法到达,则输出 −1-1。

输入输出样例

  • 输入#1

    5
    2 5
    2 2
    0 1
    2 5
    2 2
    1 0
    1 7
    3
    2
    4 212398151713
    3 2 5 2
    0 1 3 0
    2 4
    3 4
    0 0

    输出#1

    5
    6
    -1
    424796303424
    5

输入解题思路,AI测评打分。不知道怎么写?

首页