This is to factor a number quickly, if can done successfully the last problem, this is easy.
My WA code :
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pw = new PrintWriter(System.out);
static int count = 0, t;
public static void main(String[] args) throws IOException {
String[] s = br.readLine().split(" ");
int n = Integer.parseInt(s[0]);
while (n-- > 0) {
s = br.readLine().split(" ");
t = Integer.parseInt(s[0]);
for (int i = 2; i <= t; i++) {
int res[] = f(t, i);
t = res[1];
if (res[0] != 0) pw.println(i + " " + res[0]);
}
pw.println();
}
pw.flush();
br.close();
}
private static int[] f(int t, int i) {
count = 0;
while (t % i == 0) {
t = t / i;
count++;
}
return new int[]{count, t};
}
}
AC code :
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pw = new PrintWriter(System.out);
static int t;
public static void main(String[] args) throws IOException {
String[] s = br.readLine().split(" ");
int n = Integer.parseInt(s[0]);
while (n-- > 0) {
s = br.readLine().split(" ");
t = Integer.parseInt(s[0]);
f(t);
pw.println();
}
pw.flush();
br.close();
}
private static void f(int n) {
for (int i = 2; i < n / i; i++) { //PrimeNumber
if (n % i == 0) {
int count = 0;
while (n % i == 0) {
n /= i;
count++;
}
pw.println(i + " " + count);
}
}
if (n > 1) pw.println(n + " " + 1);
}
}