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 C# (Mono 4.6.2.0) (judge verdict: Runtime Error)

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
class Solve{
    public Solve(){}
    StringBuilder sb;
    ReadData re;
    public static int Main(){
        new Solve().Run();
        return 0;
    }
    void Run(){
        sb = new StringBuilder();
        re = new ReadData();
        Calc();
        Console.Write(sb.ToString());
    }
    long gcd(long x,long y){
        if(y == 0){
            return x;
        }
        else{
            return gcd(y,x % y);
        }
    }
    void Calc(){
        int N = re.i()+1;
        long[] A = new long[N];
        for(int i=0;i<N;i++){
            A[N-1-i] = re.i();
        }
        PrimeFactor Prime = new PrimeFactor(100000);
        long g = A[0];
        for(int i=1;i<N;i++){
            g = gcd(Math.Max(A[i],-A[i]),g);
        }
        List<long> ansl = new List<long>();
        {
            int[] GP = Prime.PrimeDivide(g);
            for(int i=0;i<GP.Length;i++){
                ansl.Add(GP[i]);
            }
        }
        for(int i=0;i<N;i++){
            A[i] /= g;
        }
        {
            long minnonzero = -1;
            for(int i=0;i<N;i++){
                if(A[i] != 0){
                    minnonzero = i;
                    break;
                }
            }
            int[] candi = Prime.PrimeDivide(Math.Max(A[minnonzero],-A[minnonzero]));
            for(int i=0;i<candi.Length;i++){
                if(candi[i] > N+2){
                    break;
                }
                bool OK = true;
                for(int j=1;j<candi[i];j++){ 
                    long xp = 1;
                    long sum = 0; 
                    long p = candi[i];   
                    for(int k=0;k<N;k++){
                        sum += xp*((A[k]+1000000000*p)%p);
                        sum %= p;
                        xp *= j;
                        xp %= p; 
                    }
                    OK &= (sum == 0);
                }
                if(OK){
                    ansl.Add(candi[i]);
                }
            }
        }
        long[] ans = ansl.ToArray();
        if(ans.Length == 0){
            return;
        }
        Array.Sort(ans);
        sb.Append(ans[0]+"\n");
        for(int i=1;i<ans.Length;i++){
            if(ans[i] != ans[i-1]){
                sb.Append(ans[i]+"\n");
            }
        }
    }
}
class PrimeFactor{
    int[] LowestPrimeFactor;
    int[] Prime;
    int N;
    public PrimeFactor(int N0){
        N = N0;
        N++;
        LowestPrimeFactor = new int[N];
        List<int> prime = new List<int>();
        for(int i=1;i<N;i++){
            LowestPrimeFactor[i] = i;
        }
        for(int i=2;i<N;i++){
            if(LowestPrimeFactor[i] == i){
                prime.Add(i);
            }
            for(int j=0;j<prime.Count;j++){
                if(prime[j]*i < N && prime[j] <= LowestPrimeFactor[i]){
                    LowestPrimeFactor[prime[j]*i] = prime[j];
                }
                else{
                    break;
                }
            }
        }
        Prime = prime.ToArray();
    }
    public int[] PrimeDivide(int X){
        List<int> ans = new List<int>();
        while(X != 1){
            ans.Add(LowestPrimeFactor[X]);
            X /= LowestPrimeFactor[X];
        }
        return ans.ToArray();
    }
    public int[] PrimeDivide(long X0){
        List<int> ans = new List<int>();
        int X = (int)X0;
        int i=0;
        while(X >= N){
            if(X % Prime[i] == 0){
                ans.Add(Prime[i]);
                X /= Prime[i];
            }
            else{
                i++;
                if(i == Prime.Length){
                    ans.Add(X);
                    X = 1;
                }
            }
        }
        while(X != 1){
            ans.Add(LowestPrimeFactor[X]);
            X /= LowestPrimeFactor[X];
        }
        return ans.ToArray();
    }
    public int[] Divisor(int X){
        List<int> ans = new List<int>();
        ans.Add(1);
        while(X != 1){
            int p = LowestPrimeFactor[X];
            int c = 0;
            while(X % p == 0){
                X /= p;
                c++;
            }
            int l = ans.Count;
            int d = p;
            for(int i=1;i<=c;i++){
                for(int j=0;j<l;j++){
                    ans.Add(d*ans[j]);
                }
                d *= p;
            }
        }
        return ans.ToArray();
    }
    public int[] Divisor(long X0){
        List<int> ans = new List<int>();
        int X = (int)X0;
        ans.Add(1);
        int i=0;
        while(X >= N){
            if(X % Prime[i] == 0){
                int p = Prime[i];
                int c = 0;
                while(X % p == 0){
                    X /= p;
                    c++;
                }
                int l = ans.Count;
                int d = p;
                for(int k=1;k<=c;k++){
                    for(int j=0;j<l;j++){
                        ans.Add(d*ans[j]);
                    }
                    d *= p;
                }
            }
            else{
                i++;
                if(i == Prime.Length){
                    int l = ans.Count;
                    for(int j=0;j<l;j++){
                        ans.Add(X*ans[j]);
                    }   
                    X = 1;
                }
            }
        }
        while(X != 1){
            int p = LowestPrimeFactor[X];
            int c = 0;
            while(X % p == 0){
                X /= p;
                c++;
            }
            int l = ans.Count;
            int d = p;
            for(int k=1;k<=c;k++){
                for(int j=0;j<l;j++){
                    ans.Add(d*ans[j]);
                }
                d *= p;
            }
        }
        return ans.ToArray();
    }
    public int Phi(int X){
        int ans = X;
        while(X != 1){
            int p = LowestPrimeFactor[X];
            while(X % p == 0){
                X /= p;
            }
            ans /= p;
            ans *= p-1;
        }
        return ans;
    }
    public int Phi(long X0){
        int ans = (int)X0;
        int X = (int)X0;
        int i=0;
        while(X >= N){
            if(X % Prime[i] == 0){
                int p = Prime[i];
                while(X % p == 0){
                    X /= p;
                }
                ans /= p;
                ans *= p-1;
            }
            else{
                i++;
                if(i == Prime.Length){
                    int p = X;
                    ans /= p;
                    ans *= p-1;
                    X = 1;
                }
            }
        }
        while(X != 1){
            int p = LowestPrimeFactor[X];
            while(X % p == 0){
                X /= p;
            }
            ans /= p;
            ans *= p-1;
        }
        return ans;
    }
}
class ReadData{
    string[] str;
    int counter;
    public ReadData(){
        counter = 0;
    }
    public string s(){
        if(counter == 0){
            str = Console.ReadLine().Split(' ');
            counter = str.Length;
        }
        counter--;
        return str[str.Length-counter-1];
    }
    public int i(){
        return int.Parse(s());
    }
    public long l(){
        return long.Parse(s());
    }
    public double d(){
        return double.Parse(s());
    }
    public int[] ia(int N){
        int[] ans = new int[N];
        for(int j=0;j<N;j++){
            ans[j] = i();
        }
        return ans;
    }
    public int[] ia(){
        str = Console.ReadLine().Split(' ');
        counter = 0;
        int[] ans = new int[str.Length];
        for(int j=0;j<str.Length;j++){
            ans[j] = int.Parse(str[j]);
        }
        return ans;
    }
    public long[] la(int N){
        long[] ans = new long[N];
        for(int j=0;j<N;j++){
            ans[j] = l();
        }
        return ans;
    }
    public long[] la(){
        str = Console.ReadLine().Split(' ');
        counter = 0;
        long[] ans = new long[str.Length];
        for(int j=0;j<str.Length;j++){
            ans[j] = long.Parse(str[j]);
        }
        return ans;
    }
    public double[] da(int N){
        double[] ans = new double[N];
        for(int j=0;j<N;j++){
            ans[j] = d();
        }
        return ans;
    }
    public double[] da(){
        str = Console.ReadLine().Split(' ');
        counter = 0;
        double[] ans = new double[str.Length];
        for(int j=0;j<str.Length;j++){
            ans[j] = double.Parse(str[j]);
        }
        return ans;
    }
    public List<int>[] g(int N,int[] f,int[] t){
        List<int>[] ans = new List<int>[N];
        for(int j=0;j<N;j++){
            ans[j] = new List<int>();
        }
        for(int j=0;j<f.Length;j++){
            ans[f[j]].Add(t[j]);
            ans[t[j]].Add(f[j]);
        }
        return ans;
    }
    public List<int>[] g(int N,int M){
        List<int>[] ans = new List<int>[N];
        for(int j=0;j<N;j++){
            ans[j] = new List<int>();
        }
        for(int j=0;j<M;j++){
            int f = i()-1;
            int t = i()-1;
            ans[f].Add(t);
            ans[t].Add(f);
        }
        return ans;
    }
    public List<int>[] g(){
        int N = i();
        int M = i();
        List<int>[] ans = new List<int>[N];
        for(int j=0;j<N;j++){
            ans[j] = new List<int>();
        }
        for(int j=0;j<M;j++){
            int f = i()-1;
            int t = i()-1;
            ans[f].Add(t);
            ans[t].Add(f);
        }
        return ans;
    }
}
public static class Define{
    public const long mod = 1000000007;
}
public static class Debug{
    public static void Print(double[,,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                for(int l=0;l<k.GetLength(2);l++){
                    Console.Write(k[i,j,l]+" ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
    public static void Print(double[,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                Console.Write(k[i,j]+" ");
            }
            Console.WriteLine();
        }
    }
    public static void Print(double[] k){
        for(int i=0;i<k.Length;i++){
            Console.WriteLine(k[i]);
        }
    }
    public static void Print(long[,,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                for(int l=0;l<k.GetLength(2);l++){
                    Console.Write(k[i,j,l]+" ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
    public static void Print(long[,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                Console.Write(k[i,j]+" ");
            }
            Console.WriteLine();
        }
    }
    public static void Print(long[] k){
        for(int i=0;i<k.Length;i++){
            Console.WriteLine(k[i]);
        }
    }
    public static void Print(int[,,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                for(int l=0;l<k.GetLength(2);l++){
                    Console.Write(k[i,j,l]+" ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
    public static void Print(int[,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                Console.Write(k[i,j]+" ");
            }
            Console.WriteLine();
        }
    }
    public static void Print(int[] k){
        for(int i=0;i<k.Length;i++){
            Console.WriteLine(k[i]);
        }
    }
}