Skip to content

Instantly share code, notes, and snippets.

@lavantien
Last active March 14, 2021 19:46
Show Gist options
  • Save lavantien/e090e6c63e6beb8338e058972c6d9be9 to your computer and use it in GitHub Desktop.
Save lavantien/e090e6c63e6beb8338e058972c6d9be9 to your computer and use it in GitHub Desktop.
Template for Java IO Competitive Programming. Fastest Java code yet: 0.41s
package com.lavantien;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
InputReader ir = new InputReader();
OutputWriter ow = new OutputWriter();
int t = ir.nextInt();
while (t-- > 0) {
int a = ir.nextInt();
int b = ir.nextInt();
ow.printLine(a * b);
}
ir.close();
ow.flush();
ow.close();
}
static class InputReader {
private final int BUFFER_SIZE = 1 << 16;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
public InputReader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public InputReader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
break;
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg) {
return -ret;
}
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg) {
return -ret;
}
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg) {
return -ret;
}
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) {
buffer[0] = -1;
}
}
private byte read() throws IOException {
if (bufferPointer == bytesRead) {
fillBuffer();
}
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null) {
return;
}
din.close();
}
}
private static class OutputWriter {
private final PrintWriter writer;
public OutputWriter() {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
}
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
static class Pair {
private int key;
private int value;
public Pair(int key, int value) {
this.key = key;
this.value = value;
}
public int getKey() {
return key;
}
public int getValue() {
return value;
}
public void setKey(int key) {
this.key = key;
}
public void setValue(int value) {
this.value = value;
}
}
static class CustomMath {
static long gcd(long a, long b) {
// GCD(0, b) == b; GCD(a, 0) == a,
// GCD(0, 0) == 0
if (a == 0) {
return b;
}
if (b == 0) {
return a;
}
// Finding K, where K is the greatest
// power of 2 that divides both a and b
long k;
for (k = 0; ((a | b) & 1) == 0; ++k) {
a >>= 1;
b >>= 1;
}
// Dividing a by 2 until a becomes odd
while ((a & 1) == 0) {
a >>= 1;
}
// From here on, 'a' is always odd.
do {
// If b is even, remove
// all factor of 2 in b
while ((b & 1) == 0) {
b >>= 1;
}
// Now a and b are both odd. Swap
// if necessary so a <= b, then set
// b = b - a (which is even)
if (a > b) {
// Swap u and v.
long temp = a;
a = b;
b = temp;
}
b = (b - a);
} while (b != 0);
// restore common factors of 2
return a << k;
}
static long pow(long p, long n, long mod) {
if (n == 0) {
return 1;
} else if (n % 2 == 1) {
return (p * (long)(Math.pow(pow(p, n / 2, mod), 2) % mod)) % mod;
} else {
return (long)Math.pow(pow(p, n / 2, mod), 2) % mod;
}
}
static void seive(int n, int[] prime) { //1 for prime -1 for not prime
for (int i = 2; i <= n; i++) {
if (prime[i] == 0) {
prime[i] = 1;
for (int j = 2; j * i <= n; j++) {
prime[j * i] = -1;
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment