Problem:
Score : 800 points

Problem Statement

You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x.

Constraints

0 \leq N \leq 10^4

|a_i| \leq 10^9(0\leq i\leq N)

a_N \neq 0

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_N
:
a_0

Output

Print all prime numbers p that divide f(x) for every integer x, in ascending order.

Sample Input 1

2
7
-7
14

Sample Output 1

2
7

2 and 7 divide, for example, f(1)=14 and f(2)=28.

Sample Input 2

3
1
4
1
5

Sample Output 2

There may be no integers that satisfy the condition.

Sample Input 3

0
998244353

Sample Output 3

998244353

Solution in Scala (2.11.7) (judge verdict: Wrong Answer)

import scala.annotation.tailrec
import scala.io.StdIn._
object Main extends App {

  val n = readInt
  val coefficient = Array.tabulate(n + 1){_ ⇒ readLong}
  val primes = primesUnder(100000)
  val g = coefficient.reduce(gcd)
  List(primes.takeWhile(_ <= n).filter(p ⇒ g % p == 0 || canDivide(coefficient, p)).toList, primes.withFilter(p ⇒ p > n && g % p == 0).flatMap(p ⇒ List(p, g / p)).toList, Some(g).filter(_ ⇒ g != 1 && primes.forall(g % _ != 0)).toList).flatten.distinct.sorted.dropWhile(_ == 1).foreach(println)
  def canDivide(coefficient: Array[Long], targetPrime: Long): Boolean = {
    val prime = targetPrime.toInt
    @tailrec def sub(position: Int, minus: Long = 0): Boolean = {
      if (position >= coefficient.length) minus % prime == 0
      else sub(position + prime - 1, coefficient(position) + minus)
    }
    (0 until prime - 1).forall(i ⇒ sub(i))
  }

  def gcd(a: Long, b: Long): Long = {
    @tailrec def sub(a: Long ,b: Long): Long = {
      if (b == 0L) a else sub(b, a % b)
    }
    sub(math.abs(a), math.abs(b))
  }
  def primesUnder(upperLimit: Int): Array[Long] = {
    val isPrime = Array.fill(upperLimit + 1){true}
    for (p ← 2 to math.sqrt(upperLimit).toInt if isPrime(p)){
      for (j ← p * p to upperLimit by p) isPrime(j) = false
    }
    (2 to upperLimit).filter(isPrime).map(_.toLong).toArray
  }
}