// http://www.codeeval.com/open_challenges/45/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
class Main {
public static void main(String[] args) {
reverse_add.main(args);
}
}
class reverse_add {
public static void main(String[] args) {
boolean LOCAL_TEST = false;
// LOCAL_TEST = true;// comment it before submitting
if (LOCAL_TEST)
CodeEvalGetInput("e:\\zin.txt");
else
CodeEvalGetInput(args[0]);
}
public static void CodeEvalGetInput(String arg) {
try {
File file = new File(arg);
BufferedReader in = new BufferedReader(new FileReader(file));
String line;
while ((line = in.readLine()) != null) {
String[] lineArray = line.split("\\s");
if (lineArray.length > 0) {
// Process line of input Here
Process(lineArray);
}
}
} catch (IOException e) {
System.out.println("File Read Error: " + e.getMessage());
}
}
private static void Process(String[] lineArray) {
long N = Long.valueOf(lineArray[0]);
int it = 0;
String sum = "";
while (true) {
it++;
long rev = Long.valueOf(new StringBuffer(String.valueOf(N)).reverse()
.toString());
sum = String.valueOf(N + rev);
if (sum.equals(new StringBuffer(sum).reverse().toString())) {
break;
}
N = N + rev;
}
System.out.println(it + " " + sum);
}
}
Some solution examples of problems in topcoder, codeeval, google code jam, interviewstreet, onlinejudge-uva, codeforces, projecteuler, etc
Friday, November 30, 2012
CodeEval Jolly Jumpers
// CodeEval Jolly Jumpers
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
class Main {
public static void main(String[] args) {
jolly_jumpers.main(args);
}
}
class jolly_jumpers {
public static void main(String[] args) {
CodeEvalGetInput(args[0]);
// CodeEvalGetInput("e:\\zin.txt");
}
public static void CodeEvalGetInput(String arg) {
try {
File file = new File(arg);
BufferedReader in = new BufferedReader(new FileReader(file));
String line;
while ((line = in.readLine()) != null) {
String[] lineArray = line.split("\\s");
if (lineArray.length > 0) {
// Process line of input Here
Process(lineArray);
}
}
} catch (IOException e) {
System.out.println("File Read Error: " + e.getMessage());
}
}
private static void Process(String[] lineArray) {
int N = Integer.valueOf(lineArray[0]);
Set<Integer> sets = new HashSet<Integer>();
boolean jolly = true;
for (int i = 1; i < N; i++) {
int diff = Integer.valueOf(lineArray[i + 1])
- Integer.valueOf(lineArray[i]);
diff = Math.abs(diff);
if (diff >= N || sets.contains(diff)) {
jolly = false;
break;
} else {
sets.add(diff);
}
}
System.out.println(jolly ? "Jolly" : "Not jolly");
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
class Main {
public static void main(String[] args) {
jolly_jumpers.main(args);
}
}
class jolly_jumpers {
public static void main(String[] args) {
CodeEvalGetInput(args[0]);
// CodeEvalGetInput("e:\\zin.txt");
}
public static void CodeEvalGetInput(String arg) {
try {
File file = new File(arg);
BufferedReader in = new BufferedReader(new FileReader(file));
String line;
while ((line = in.readLine()) != null) {
String[] lineArray = line.split("\\s");
if (lineArray.length > 0) {
// Process line of input Here
Process(lineArray);
}
}
} catch (IOException e) {
System.out.println("File Read Error: " + e.getMessage());
}
}
private static void Process(String[] lineArray) {
int N = Integer.valueOf(lineArray[0]);
Set<Integer> sets = new HashSet<Integer>();
boolean jolly = true;
for (int i = 1; i < N; i++) {
int diff = Integer.valueOf(lineArray[i + 1])
- Integer.valueOf(lineArray[i]);
diff = Math.abs(diff);
if (diff >= N || sets.contains(diff)) {
jolly = false;
break;
} else {
sets.add(diff);
}
}
System.out.println(jolly ? "Jolly" : "Not jolly");
}
}
CodeEval Prime Numbers
// CodeEval Prime Numbers
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
class Main {
public static void main(String[] args) {
prime_less.main(args);
}
}
class prime_less {
public static void main(String[] args) {
CodeEvalGetInput(args[0]);
//CodeEvalGetInput("e:\\zin.txt");
}
public static void CodeEvalGetInput(String arg) {
try {
File file = new File(arg);
BufferedReader in = new BufferedReader(new FileReader(file));
String line;
while ((line = in.readLine()) != null) {
String[] lineArray = line.split("\\s");
if (lineArray.length > 0) {
// Process line of input Here
Process(lineArray);
}
}
} catch (IOException e) {
System.out.println("File Read Error: " + e.getMessage());
}
}
private static void Process(String[] lineArray) {
long N = Long.valueOf(lineArray[0]);
List<Long> pp = GetPrimeNumsLessThanOrEqual2(N - 1);
for (int i = 0; i < pp.size(); i++) {
if (i > 0)
System.out.print(",");
System.out.print(pp.get(i));
}
System.out.println("");
}
public static List<Long> GetPrimeNumsLessThanOrEqual2(long maxNum) {
List<Long> p = new ArrayList<Long>();
if (maxNum < 2)
return p;
p.add(2L);
for (long j = 3; j <= maxNum; j += 2) {
boolean isPrime = true;
int N = p.size();
for (int i = 0; i < N; i++) {
long pp = p.get(i);
if (pp * pp > j)
break;
if (j % pp == 0) {
isPrime = false;
break;
}
}
if (isPrime)
p.add(j);
}
return p;
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
class Main {
public static void main(String[] args) {
prime_less.main(args);
}
}
class prime_less {
public static void main(String[] args) {
CodeEvalGetInput(args[0]);
//CodeEvalGetInput("e:\\zin.txt");
}
public static void CodeEvalGetInput(String arg) {
try {
File file = new File(arg);
BufferedReader in = new BufferedReader(new FileReader(file));
String line;
while ((line = in.readLine()) != null) {
String[] lineArray = line.split("\\s");
if (lineArray.length > 0) {
// Process line of input Here
Process(lineArray);
}
}
} catch (IOException e) {
System.out.println("File Read Error: " + e.getMessage());
}
}
private static void Process(String[] lineArray) {
long N = Long.valueOf(lineArray[0]);
List<Long> pp = GetPrimeNumsLessThanOrEqual2(N - 1);
for (int i = 0; i < pp.size(); i++) {
if (i > 0)
System.out.print(",");
System.out.print(pp.get(i));
}
System.out.println("");
}
public static List<Long> GetPrimeNumsLessThanOrEqual2(long maxNum) {
List<Long> p = new ArrayList<Long>();
if (maxNum < 2)
return p;
p.add(2L);
for (long j = 3; j <= maxNum; j += 2) {
boolean isPrime = true;
int N = p.size();
for (int i = 0; i < N; i++) {
long pp = p.get(i);
if (pp * pp > j)
break;
if (j % pp == 0) {
isPrime = false;
break;
}
}
if (isPrime)
p.add(j);
}
return p;
}
}
CodeEval Prime Palindrome
// CodeEval Prime Palindrome
class prime_palindrome {
public static void main(String[] args) {
int ans = 2;
for (int i = 2; i <= 1000; i++) {
if (IsPrime(i)) {
String s = String.valueOf(i);
String r = new StringBuffer(s).reverse().toString();
if (r.equals(s))
ans = i;
}
}
System.out.println(ans);
}
public static boolean IsPrime(long num) {
if (num < 2)
return false;
if (num == 2 || num == 3)
return true;
if (num % 2 == 0)
return false;
long sqrtnum = (long) Math.sqrt(num) + 1;
for (long x = 3; x <= sqrtnum; x += 2) {
if (num % x == 0)
return false;
}
return true;
}
}
class prime_palindrome {
public static void main(String[] args) {
int ans = 2;
for (int i = 2; i <= 1000; i++) {
if (IsPrime(i)) {
String s = String.valueOf(i);
String r = new StringBuffer(s).reverse().toString();
if (r.equals(s))
ans = i;
}
}
System.out.println(ans);
}
public static boolean IsPrime(long num) {
if (num < 2)
return false;
if (num == 2 || num == 3)
return true;
if (num % 2 == 0)
return false;
long sqrtnum = (long) Math.sqrt(num) + 1;
for (long x = 3; x <= sqrtnum; x += 2) {
if (num % x == 0)
return false;
}
return true;
}
}
Online Judge UVa 10139 Factovisors
// Online Judge UVa 10139 Factovisors: Accepted
// http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1080
import java.io.FileInputStream;
import java.io.IOException;
import java.math.*;
import java.util.*;
import java.util.Map.Entry;
class Main {
private static Scanner in;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;
// LOCAL_TEST = true;
in = new Scanner(System.in);
if (LOCAL_TEST) {
in = new Scanner(new FileInputStream("E:\\zin.txt"));
}
while (in.hasNextLong()) {
long N = in.nextLong();
long M = in.nextLong();
boolean divides = true;
if (M > N) {
Map<Long, Long> mfactors = GetPrimeFactorsAsMap(M);
Set<Entry<Long, Long>> mf = mfactors.entrySet();
Map<Long, Long> nfactors = GetPrimeFactorsOfFact(N, mfactors);
// Set<Long> sss = nfactors.keySet();
// for (Long k : sss) {
// System.out.print(k + "^" + nfactors.get(k) + " ");
// }
// System.out.println();
for (Entry<Long, Long> e : mf) {
Long prime = e.getKey();
Long exp = e.getValue();
if (!nfactors.containsKey(prime)) {
divides = false;
break;
} else if (nfactors.get(prime) < exp) {
divides = false;
break;
}
}
}
if (divides)
System.out.println(M + " divides " + N + "!");
else
System.out.println(M + " does not divide " + N + "!");
}
}
public static List<Long> GetPrimeFactors(long num) {
List<Long> factors = new ArrayList<Long>();
long n = num;
while (n % 2 == 0) {
factors.add(2L);
n = n / 2;
}
long sqrtn = (long) Math.sqrt(n) + 1;
long i = 3;
while (i < sqrtn) {
if (n % i == 0) {
factors.add(i);
n = n / i;
} else {
i = i + 2;
}
}
if (n > 1)
factors.add(n);
return factors;
}
public static Map<Long, Long> GetPrimeFactorsAsMap(long x) {
Map<Long, Long> dict = new LinkedHashMap<Long, Long>();
List<Long> f = GetPrimeFactors(x);
for (Long n : f) {
if (!dict.containsKey(n))
dict.put(n, 1L);
else
dict.put(n, dict.get(n) + 1);
}
return dict;
}
public static Map<Long, Long> GetPrimeFactorsOfFact(long x,
Map<Long, Long> mfactors) {
Map<Long, Long> dict = new LinkedHashMap<Long, Long>();
// List<Long> primes = GetPrimeNumsLessThanOrEqual2(sx);
List<Long> primes = new ArrayList<Long>();
primes.addAll(mfactors.keySet());
for (Long p : primes) {
long pk = p;
long nfactors = 0;
while (true) {
if (x / pk > 0) {
nfactors += (x / pk);
pk = pk * p;
} else
break;
}
if (nfactors > 0)
dict.put(p, nfactors);
}
return dict;
}
public static List<Long> GetPrimeNumsLessThanOrEqual2(long maxNum) {
List<Long> p = new ArrayList<Long>();
if (maxNum < 2)
return p;
p.add(2L);
for (long j = 3; j <= maxNum; j += 2) {
boolean isPrime = true;
int N = p.size();
for (int i = 0; i < N; i++) {
long pp = p.get(i);
if (pp * pp > j)
break;
if (j % pp == 0) {
isPrime = false;
break;
}
}
if (isPrime)
p.add(j);
}
return p;
}
}
// http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1080
import java.io.FileInputStream;
import java.io.IOException;
import java.math.*;
import java.util.*;
import java.util.Map.Entry;
class Main {
private static Scanner in;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;
// LOCAL_TEST = true;
in = new Scanner(System.in);
if (LOCAL_TEST) {
in = new Scanner(new FileInputStream("E:\\zin.txt"));
}
while (in.hasNextLong()) {
long N = in.nextLong();
long M = in.nextLong();
boolean divides = true;
if (M > N) {
Map<Long, Long> mfactors = GetPrimeFactorsAsMap(M);
Set<Entry<Long, Long>> mf = mfactors.entrySet();
Map<Long, Long> nfactors = GetPrimeFactorsOfFact(N, mfactors);
// Set<Long> sss = nfactors.keySet();
// for (Long k : sss) {
// System.out.print(k + "^" + nfactors.get(k) + " ");
// }
// System.out.println();
for (Entry<Long, Long> e : mf) {
Long prime = e.getKey();
Long exp = e.getValue();
if (!nfactors.containsKey(prime)) {
divides = false;
break;
} else if (nfactors.get(prime) < exp) {
divides = false;
break;
}
}
}
if (divides)
System.out.println(M + " divides " + N + "!");
else
System.out.println(M + " does not divide " + N + "!");
}
}
public static List<Long> GetPrimeFactors(long num) {
List<Long> factors = new ArrayList<Long>();
long n = num;
while (n % 2 == 0) {
factors.add(2L);
n = n / 2;
}
long sqrtn = (long) Math.sqrt(n) + 1;
long i = 3;
while (i < sqrtn) {
if (n % i == 0) {
factors.add(i);
n = n / i;
} else {
i = i + 2;
}
}
if (n > 1)
factors.add(n);
return factors;
}
public static Map<Long, Long> GetPrimeFactorsAsMap(long x) {
Map<Long, Long> dict = new LinkedHashMap<Long, Long>();
List<Long> f = GetPrimeFactors(x);
for (Long n : f) {
if (!dict.containsKey(n))
dict.put(n, 1L);
else
dict.put(n, dict.get(n) + 1);
}
return dict;
}
public static Map<Long, Long> GetPrimeFactorsOfFact(long x,
Map<Long, Long> mfactors) {
Map<Long, Long> dict = new LinkedHashMap<Long, Long>();
// List<Long> primes = GetPrimeNumsLessThanOrEqual2(sx);
List<Long> primes = new ArrayList<Long>();
primes.addAll(mfactors.keySet());
for (Long p : primes) {
long pk = p;
long nfactors = 0;
while (true) {
if (x / pk > 0) {
nfactors += (x / pk);
pk = pk * p;
} else
break;
}
if (nfactors > 0)
dict.put(p, nfactors);
}
return dict;
}
public static List<Long> GetPrimeNumsLessThanOrEqual2(long maxNum) {
List<Long> p = new ArrayList<Long>();
if (maxNum < 2)
return p;
p.add(2L);
for (long j = 3; j <= maxNum; j += 2) {
boolean isPrime = true;
int N = p.size();
for (int i = 0; i < N; i++) {
long pp = p.get(i);
if (pp * pp > j)
break;
if (j % pp == 0) {
isPrime = false;
break;
}
}
if (isPrime)
p.add(j);
}
return p;
}
}
Subscribe to:
Posts (Atom)