题目链接,本代码通过了dotcpp中的所有测试点
import java.io.*;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static StringBuilder sb = new StringBuilder();
static int N = 100005;
static int t;
static int[] wood = new int[N];
static boolean[] win = new boolean[N];
static int cnt = 0;
static int[] primes = new int[N];
static boolean[] st = new boolean[N];
public static void getPrimes(int n) {
for (int i = 2; i <= n; i++) {
if (!st[i])
primes[cnt++] = i;
for (int j = 0; primes[j] * i <= n; j++) {
int t = primes[j] * i;
st[t] = true;
if (i % primes[j] == 0)
break;
}
}
}
public static int nearest(int x) { // 求小于x的最大质数
int l = 0, r = cnt - 1;
while (l < r) {
int mid = (l + r + 1) / 2;
if (primes[mid] > x)
r = mid - 1;
else
l = mid;
}
return l;
}
public static void main(String[] args) throws IOException {
t = Integer.parseInt(in.readLine());
int maxLen = 0;
for (int i = 0; i < t; i++) {
wood[i] = Integer.parseInt(in.readLine());
maxLen = Math.max(maxLen, wood[i]);
}
getPrimes(maxLen);
for (int i = 2; i <= maxLen; i++) {
for (int j = nearest(i); j >= 0; j--) {
if (!win[i - primes[j]]) {
win[i] = true;
break;
}
}
}
for (int i = 0; i < t; i++) {
if (win[wood[i]])
sb.append("1\n");
else
sb.append("0\n");
}
System.out.print(sb);
in.close();
}
}