首页 > 代码库 > URAL 1086 Cryptography

URAL 1086 Cryptography

打质数表,记得很早之前优化过,但是忘了……

 1 import java.util.ArrayList; 2 import java.util.List; 3 import java.util.Scanner; 4  5 public class P1086 6 { 7     private static final int MAXN = 200000; 8     private static boolean isPrime[] = new boolean[MAXN]; 9     private static List<Integer> primeList = new ArrayList<>();10 11     public static void initPrimeList()12     {13         for (int i = 2; i < MAXN; i++)14             isPrime[i] = true;15         for (int i = 2; i < MAXN; i++)16             if (isPrime[i])17             {18                 for (int j = 2; i * j < MAXN; j++)19                     isPrime[i * j] = false;20                 primeList.add(i);21             }22         //        System.out.println(primeList.size());23     }24 25     public static void main(String args[])26     {27         initPrimeList();28         try (Scanner cin = new Scanner(System.in))29         {30             while (cin.hasNext())31             {32                 int n = cin.nextInt();33                 for (int i = 0; i < n; i++)34                     System.out.println(primeList.get(cin.nextInt() - 1));35             }36         }37     }38 }

 

URAL 1086 Cryptography