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

#SP0000062. Nguyên tố thứ n trong cấp số cộng (Dirichlet's Theorem on Arithmetic Progressions)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Nguyên tố thứ n trong cấp số cộng (Dirichlet's Theorem on Arithmetic Progressions)

Dirichlet's Theorem on Arithmetic Progressions

Source: Aizu Online Judge

Version: Phuoc Hung OJ Extended

Problem Statement

Given coprime positive integers a,da,d, consider the infinite arithmetic progression a,a+d,a+2d,a+3d,…a,a+d,a+2d,a+3d,\ldots. Keep only prime terms, preserving their original order. Given a positive integer nn, find the nn-th prime in this filtered sequence. Dirichlet’s theorem guarantees infinitely many such primes when gcd⁡(a,d)=1\gcd(a,d)=1; the input guarantees the answer is less than 10610^6.

Input

One line contains three positive integers a,d,na,d,n, in that order.

Output

Print one integer: the nn-th prime in the given arithmetic progression.

Subtasks

  • Subtask 1 (20 points): 1≤a≤1001\le a\le 100; 1≤d≤201\le d\le 20; 1≤n≤101\le n\le 10; gcd⁡(a,d)=1\gcd(a,d)=1; the answer is less than 10610^6.
  • Subtask 2 (30 points): 1≤a≤10001\le a\le 1000; 1≤d≤1001\le d\le 100; 1≤n≤801\le n\le 80; gcd⁡(a,d)=1\gcd(a,d)=1; the answer is less than 10610^6.
  • Subtask 3 (50 points): 1≤a≤93071\le a\le 9307; 1≤d≤3461\le d\le 346; 1≤n≤2101\le n\le 210; gcd⁡(a,d)=1\gcd(a,d)=1; the answer is less than 10610^6.

Examples

Input

2 3 3

Output

11

Explanation

The progression starts 2,5,8,11,14,17,…2,5,8,11,14,17,\ldots. Its prime terms are 22 (first), 55 (second), 1111 (third) and 1717 (fourth). Since n=3n=3, print the third prime, 11.