B - Dividing Subsequence Editorial /

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 500500

問題文

(1,2,,N)(1,2,\cdots,N) の順列 P=(P1,P2,,PN)P=(P_1,P_2,\cdots,P_N) および Q=(Q1,Q2,,QN)Q=(Q_1,Q_2,\cdots,Q_N) が与えられます.

すぬけくんは,PPQQ から(連続するとは限らない)部分列を取り出そうとしています. ここで,取り出した部分列は以下の条件を満たす必要があります.

  • PP から取り出した部分列と QQ から取り出した部分列の長さは等しい.以下,この長さを kk とおく.
  • PP から取り出した部分列を a=(a1,a2,,ak)a=(a_1,a_2,\cdots,a_k)QQ から取り出した部分列を b=(b1,b2,,bk)b=(b_1,b_2,\cdots,b_k) とおく. このとき,各 1ik1 \leq i \leq k について,bib_iaia_i の倍数である.

すぬけ君が取り出せる部分列の長さの最大値を求めて下さい.

制約

  • 1N2000001 \leq N \leq 200000
  • PP(1,2,,N)(1,2,\cdots,N) の順列である
  • QQ(1,2,,N)(1,2,\cdots,N) の順列である
  • 入力される値はすべて整数である

入力

入力は以下の形式で標準入力から与えられる.

NN
P1P_1 P2P_2 \cdots PNP_N
Q1Q_1 Q2Q_2 \cdots QNQ_N

出力

答えを出力せよ.


入力例 1Copy

Copy
4
3 1 4 2
4 2 1 3

出力例 1Copy

Copy
2

PP から部分列 (1,2)(1,2) を,QQ から部分列 (4,2)(4,2) を取り出すと,これは条件を満たします. 長さ 33 以上の部分列を条件を満たすように取ることはできないため,答えは 22 です.


入力例 2Copy

Copy
5
1 2 3 4 5
5 4 3 2 1

出力例 2Copy

Copy
3

入力例 3Copy

Copy
10
4 3 1 10 9 2 8 6 5 7
9 6 5 4 2 3 8 10 1 7

出力例 3Copy

Copy
6

Score : 500500 points

Problem Statement

Given are permutations P=(P1,P2,,PN)P=(P_1,P_2,\cdots,P_N) and Q=(Q1,Q2,,QN)Q=(Q_1,Q_2,\cdots,Q_N) of (1,2,,N)(1,2,\cdots,N).

Snuke is going to extract (not necessarily contiguous) subsequences from PP and QQ. Here, the subsequences must satisfy the conditions below.

  • The length of the subsequence extracted from PP and that extracted from QQ are the same. Below, let kk be this length.
  • Let a=(a1,a2,,ak)a=(a_1,a_2,\cdots,a_k) and b=(b1,b2,,bk)b=(b_1,b_2,\cdots,b_k) be the subsequences extracted from PP and QQ, respectively. Then, for each 1ik1 \leq i \leq k, bib_i is a multiple of aia_i.

Find the maximum possible length of each subsequence extracted by Snuke.

Constraints

  • 1N2000001 \leq N \leq 200000
  • PP is a permutation of (1,2,,N)(1,2,\cdots,N).
  • QQ is a permutation of (1,2,,N)(1,2,\cdots,N).
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN
P1P_1 P2P_2 \cdots PNP_N
Q1Q_1 Q2Q_2 \cdots QNQ_N

Output

Print the answer.


Sample Input 1Copy

Copy
4
3 1 4 2
4 2 1 3

Sample Output 1Copy

Copy
2

If we extract the subsequence (1,2)(1,2) from PP and (4,2)(4,2) from QQ, they satisfy the conditions. It is impossible to extract subsequences of length 33 or greater to satisfy the conditions, so the answer is 22.


Sample Input 2Copy

Copy
5
1 2 3 4 5
5 4 3 2 1

Sample Output 2Copy

Copy
3

Sample Input 3Copy

Copy
10
4 3 1 10 9 2 8 6 5 7
9 6 5 4 2 3 8 10 1 7

Sample Output 3Copy

Copy
6


2025-04-29 (Tue)
01:13:07 +00:00