Created
January 5, 2018 12:41
-
-
Save chermehdi/363b30aa57bc09526716557059fc796d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.OutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.PrintWriter; | |
import java.io.OutputStream; | |
import java.util.Arrays; | |
import java.io.InputStreamReader; | |
import java.io.FileNotFoundException; | |
import java.io.File; | |
import java.util.StringTokenizer; | |
import java.io.Writer; | |
import java.io.BufferedReader; | |
import java.io.UnsupportedEncodingException; | |
import java.io.InputStream; | |
/** | |
* Built using CHelper plug-in | |
* Actual solution is at the top | |
* | |
* @author MaxHeap | |
*/ | |
public class Main { | |
public static void main(String[] args) { | |
InputStream inputStream = System.in; | |
OutputStream outputStream = System.out; | |
FastReader in = new FastReader(inputStream); | |
OutputWriter out = new OutputWriter(outputStream); | |
Azuz solver = new Azuz(); | |
int testCount = Integer.parseInt(in.next()); | |
for (int i = 1; i <= testCount; i++) | |
solver.solve(i, in, out); | |
out.close(); | |
} | |
static class Azuz { | |
char[] s; | |
int n; | |
boolean[] isPrime; | |
int MAX = (int) 1e6 + 10; | |
int[] dp; | |
{ | |
isPrime = new boolean[MAX]; | |
Arrays.fill(isPrime, true); | |
isPrime[0] = isPrime[1] = false; | |
for (int i = 2; i < MAX; i++) { | |
if (isPrime[i]) { | |
for (int j = i * 2; j < MAX; j += i) { | |
isPrime[j] = false; | |
} | |
} | |
} | |
} | |
int rec(int i) { | |
if (i == n) return 0; | |
if (i > n) return MAX; | |
if (dp[i] != -1) return dp[i]; | |
int ans = MAX; | |
int cur = 0; | |
for (int j = 0; j < 6 && i + j < n; j++) { | |
cur = 10 * cur + s[i + j] - '0'; | |
if (isPrime[cur]) { | |
ans = Math.min(ans, rec(i + j + 1) + 1); | |
} | |
} | |
return dp[i] = ans; | |
} | |
public void solve(int testNumber, FastReader in, OutputWriter out) { | |
s = in.next().toCharArray(); | |
n = s.length; | |
dp = new int[n + 1]; | |
Arrays.fill(dp, -1); | |
out.println(rec(0)); | |
} | |
} | |
static class OutputWriter extends PrintWriter { | |
public OutputWriter(OutputStream os, boolean autoFlush) { | |
super(os, autoFlush); | |
} | |
public OutputWriter(Writer out) { | |
super(out); | |
} | |
public OutputWriter(Writer out, boolean autoFlush) { | |
super(out, autoFlush); | |
} | |
public OutputWriter(String fileName) throws FileNotFoundException { | |
super(fileName); | |
} | |
public OutputWriter(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException { | |
super(fileName, csn); | |
} | |
public OutputWriter(File file) throws FileNotFoundException { | |
super(file); | |
} | |
public OutputWriter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException { | |
super(file, csn); | |
} | |
public OutputWriter(OutputStream out) { | |
super(out); | |
} | |
public void flush() { | |
super.flush(); | |
} | |
public void close() { | |
super.close(); | |
} | |
} | |
static class FastReader { | |
BufferedReader reader; | |
StringTokenizer st; | |
public FastReader(InputStream stream) { | |
reader = new BufferedReader(new InputStreamReader(stream)); | |
st = null; | |
} | |
public String next() { | |
while (st == null || !st.hasMoreTokens()) { | |
try { | |
String line = reader.readLine(); | |
if (line == null) { | |
return null; | |
} | |
st = new StringTokenizer(line); | |
} catch (Exception e) { | |
throw new RuntimeException(); | |
} | |
} | |
return st.nextToken(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment