#GL0000159. Bài toán GCD đơn giản (bản khó) (A Simple GCD Problem (Hard Version))
Bài tập thuộc lộ trình
Mục lục đề ⌄
Bài toán GCD đơn giản (bản khó) (A Simple GCD Problem (Hard Version))
A Simple GCD Problem (Hard Version)
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given positive-integer arrays and , each of length . At most once per position , you may replace by a different positive integer with . Here is independent of and may be smaller or larger.
After all operations, the GCD of every contiguous subarray of length at least two must be exactly the same as before. Formally, for all ,
Maximize the number of changed positions. If you do not change index , its original value is allowed even when .
Input
The first line contains , followed by a line with and a line with .
Output
Print the maximum number of positions that can be changed.
Subtasks
- Subtask 1 (20 points): ; .
- Subtask 2 (30 points): ; .
- Subtask 3 (50 points): ; .
Examples
Input
3
2 2 2
4 4 4
Output
2
Explanation
Initially every adjacent GCD and the whole-array GCD equal . Replacing the first and third values by yields , which preserves them all and makes two changes. Changing all three values is impossible while maintaining both adjacent GCDs exactly equal to .