Skip to content

Instantly share code, notes, and snippets.

@Bekt
Created April 15, 2013 02:05
Show Gist options
  • Save Bekt/5385192 to your computer and use it in GitHub Desktop.
Save Bekt/5385192 to your computer and use it in GitHub Desktop.
Code Jam 2013, Problem C (large input 1) http://code.google.com/codejam/contest/2270488/dashboard#s=p2
import java.util.*;
import java.io.*;
public class C2 {
static Scanner in;
static BufferedWriter out;
long[] palin = new long[40];
public static void main(String[] args) throws Exception {
in = new Scanner(new File("c2.in"));
out = new BufferedWriter(new FileWriter("c2.out"));
new C2().run();
out.close();
}
void run() throws IOException {
init();
int t = in.nextInt();
for(int i=1; i<=t; i++) {
long a = in.nextLong(), b = in.nextLong();
int count = 0;
for (int j=0; j<palin.length; j++) {
if(palin[j] >= a && palin[j] <= b) {
count++;
}
}
out.write(String.format("Case #%d: %d%n", i, count));
}
}
void init() {
int j = 0;
for(long i=1; i<10000003; i++) {
if(isPalin(i) && isPalin(i * i)) {
palin[j++] = i * i;
}
}
}
boolean isPalin(long n) {
if (n < 10) {
return true;
}
String value = String.valueOf(n);
int size = value.length();
for (int i = 0, j = size - 1; i < size / 2; i++, j--) {
if (value.charAt(i) != value.charAt(j)) {
return false;
}
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment