PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000196. Số xấu thứ N với ba ước số (Ugly Number III)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Số xấu thứ N với ba ước số (Ugly Number III)

Ugly Number III

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Given four positive integers n,a,b,cn,a,b,c, call a positive integer ugly if it is divisible by at least one of a,b,ca,b,c.

List all ugly numbers in strictly increasing order, starting from position 1, and output the nn-th one. A number divisible by several of these divisors is counted only once.

The answer is guaranteed not to exceed 2⋅1092\cdot10^9.

Input

  • One line containing four integers nn, aa, bb, cc.

Output

  • Print the nn-th ugly number.

Subtasks

  • Subtask 1 (20 points): 1≤n≤10001\le n\le1000; 1≤a,b,c≤10001\le a,b,c\le1000; abc≤1018abc\le10^{18}; answer ≤2⋅109\le2\cdot10^9.
  • Subtask 2 (30 points): 1≤n≤1061\le n\le10^6; 1≤a,b,c≤1061\le a,b,c\le10^6; abc≤1018abc\le10^{18}; answer ≤2⋅109\le2\cdot10^9.
  • Subtask 3 (50 points): 1≤n,a,b,c≤1091\le n,a,b,c\le10^9; abc≤1018abc\le10^{18}; answer ≤2⋅109\le2\cdot10^9.

Examples

Input

3 2 3 5

Output

4

Explanation

The first eligible numbers are 2,3,4,5,6,…2,3,4,5,6,\ldots. Their positions start at 1, so the third number is 4. A common multiple, such as 66, is counted once, not twice.