#P3708. Recurrent Function

    ID: 2717 远端评测题 1000ms 64MiB 尝试: 0 已通过: 0 难度: (无) 上传者: 标签>POJ Founder Monthly Contest – 2008.12.28, Yaojinyu

Recurrent Function

Description

Dr. Yao is involved in a secret research on the topic of the properties of recurrent function. Some of the functions in this research are in the following pattern:

\begin{tabular} {ll} \textit{f}(\textit{j}) = \textit{a}<span>$</span>_j<span>$</span> & for 1<span>$</span>\le<span>$</span>\textit{j}<span>$</span><<span>$</span>\textit{d}, \\ \emph{f}(\emph{d}<span>$</span>\times<span>$</span>\emph{n}+\emph{j}) = d<span>$</span>\times<span>$</span>f(\textit{n})+\textit{b}<span>$</span>_j<span>$</span> & for 0<span>$</span>\le<span>$</span>\textit{j}<span>$</span><<span>$</span>\textit{d} and \textit{n}<span>$</span>\ge<span>$</span>1, \\ \end{tabular}

in which set {ai} = {1, 2, …, d-1} and {bi} = {0, 1, …, d-1}.
We denote:

\begin{tabular}{l}\emph{f}<span>$</span>_x<span>$</span>(\emph{m}) = \emph{f}(\emph{f}(\emph{f}(<span>$</span>\cdots<span>$</span>\emph{f}(\emph{m})))) \quad\emph{x} times \\ \end{tabular}

Yao's question is that, given two positive integer m and k, could you find a minimal non-negative integer x that

\begin{tabular}{l}\emph{f}<span>$</span>_x<span>$</span>(\emph{m}) = \emph{k}\\ \end{tabular}

Input

There are several test cases. The first line of each test case contains an integer d (2≤d≤100). The second line contains 2d-1 integers: a1, …, ad-1, followed by b0, ..., bd-1. The third line contains integer m (0<m≤10100), and the forth line contains integer k (0<k≤10100). The input file ends with integer -1.

Output

For each test case if it exists such an integer x, output the minimal one. We guarantee the answer is less than 263. Otherwise output a word "NO".

2
1
1 0
4
7
2
1
0 1
100
200
-1
1
NO

Hint

For the first sample case, we can see that f(4)=7. And for the second one, the function is f(i)=i.

Source

POJ Founder Monthly Contest – 2008.12.28, Yaojinyu