Last active
August 3, 2020 16:53
-
-
Save Habibu-R-ahman/14ed462e73e77daf9ba50ab87a28e65b to your computer and use it in GitHub Desktop.
Fast I/O in Java
This file contains 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.*; | |
import java.util.InputMismatchException; | |
class fast | |
{ | |
public static void main(String[] args) throws Exception | |
{ | |
InputReader in = new InputReader(System.in); | |
OutputWriter out = new OutputWriter(System.out); | |
int i = in.readInt();//READ INPUT | |
out.printLine(i);//PRINT OUTPUT | |
{ | |
out.close(); | |
} | |
} | |
} | |
//FAST IO | |
class InputReader | |
{ | |
private InputStream stream; | |
private byte[] buf = new byte[1024]; | |
private int curChar; | |
private int numChars; | |
private SpaceCharFilter filter; | |
public InputReader(InputStream stream) | |
{ | |
this.stream = stream; | |
} | |
public int read() | |
{ | |
if (numChars == -1) | |
throw new InputMismatchException(); | |
if (curChar >= numChars) | |
{ | |
curChar = 0; | |
try | |
{ | |
numChars = stream.read(buf); | |
} | |
catch (IOException e) | |
{ | |
throw new InputMismatchException(); | |
} | |
if (numChars <= 0) | |
return -1; | |
} | |
return buf[curChar++]; | |
} | |
public int readInt() | |
{ | |
int c = read(); | |
while (isSpaceChar(c)) | |
c = read(); | |
int sgn = 1; | |
if (c == '-') | |
{ | |
sgn = -1; | |
c = read(); | |
} | |
int res = 0; | |
do | |
{ | |
if (c < '0' || c > '9') | |
throw new InputMismatchException(); | |
res *= 10; | |
res += c - '0'; | |
c = read(); | |
} while (!isSpaceChar(c)); | |
return res * sgn; | |
} | |
public String readString() | |
{ | |
int c = read(); | |
while (isSpaceChar(c)) | |
c = read(); | |
StringBuilder res = new StringBuilder(); | |
do | |
{ | |
res.appendCodePoint(c); | |
c = read(); | |
} while (!isSpaceChar(c)); | |
return res.toString(); | |
} | |
public double readDouble() { | |
int c = read(); | |
while (isSpaceChar(c)) | |
c = read(); | |
int sgn = 1; | |
if (c == '-') | |
{ | |
sgn = -1; | |
c = read(); | |
} | |
double res = 0; | |
while (!isSpaceChar(c) && c != '.') | |
{ | |
if (c == 'e' || c == 'E') | |
return res * Math.pow(10, readInt()); | |
if (c < '0' || c > '9') | |
throw new InputMismatchException(); | |
res *= 10; | |
res += c - '0'; | |
c = read(); | |
} | |
if (c == '.') | |
{ | |
c = read(); | |
double m = 1; | |
while (!isSpaceChar(c)) | |
{ | |
if (c == 'e' || c == 'E') | |
return res * Math.pow(10, readInt()); | |
if (c < '0' || c > '9') | |
throw new InputMismatchException(); | |
m /= 10; | |
res += (c - '0') * m; | |
c = read(); | |
} | |
} | |
return res * sgn; | |
} | |
public long readLong() | |
{ | |
int c = read(); | |
while (isSpaceChar(c)) | |
c = read(); | |
int sgn = 1; | |
if (c == '-') | |
{ | |
sgn = -1; | |
c = read(); | |
} | |
long res = 0; | |
do | |
{ | |
if (c < '0' || c > '9') | |
throw new InputMismatchException(); | |
res *= 10; | |
res += c - '0'; | |
c = read(); | |
} while (!isSpaceChar(c)); | |
return res * sgn; | |
} | |
public boolean isSpaceChar(int c) | |
{ | |
if (filter != null) | |
return filter.isSpaceChar(c); | |
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; | |
} | |
public String next() | |
{ | |
return readString(); | |
} | |
public interface SpaceCharFilter | |
{ | |
public boolean isSpaceChar(int ch); | |
} | |
} | |
class OutputWriter | |
{ | |
private final PrintWriter writer; | |
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(); | |
} | |
} | |
/* | |
>>USAGE | |
//initialize | |
InputReader in = new InputReader(System.in); | |
OutputWriter out = new OutputWriter(System.out); | |
//read int | |
int i = in.readInt(); | |
//read string | |
String s = in.readString(); | |
//read int array of size N | |
int[] x = IOUtils.readIntArray(in,N); | |
//printline | |
out.printLine("X"); | |
//flush output | |
out.flush(); | |
//remember to close the | |
//outputstream, at the end | |
out.close(); | |
*/ | |
/* | |
class IOUtils | |
{ | |
public static int[] readIntArray(InputReader in, int size) | |
{ | |
int[] array = new int[size]; | |
for (int i = 0; i < size; i++) | |
array[i] = in.readInt(); | |
return array; | |
} | |
} | |
*/ |
This file contains 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.*; | |
import java.util.InputMismatchException; | |
/** | |
* Created by Shreyans on $DATE at $TIME using IntelliJ IDEA (Fast IO Template) | |
*/ | |
class $NAME | |
{ | |
public static void main(String[] args) throws Exception | |
{ | |
InputReader in = new InputReader(System.in); | |
OutputWriter out = new OutputWriter(System.out); | |
out.printLine("Hello World"); | |
{ | |
out.close(); | |
} | |
} | |
//FAST IO | |
private static class InputReader | |
{ | |
private InputStream stream; | |
private byte[] buf = new byte[1024]; | |
private int curChar; | |
private int numChars; | |
private SpaceCharFilter filter; | |
public InputReader(InputStream stream) | |
{ | |
this.stream = stream; | |
} | |
public int read() | |
{ | |
if (numChars == -1) | |
throw new InputMismatchException(); | |
if (curChar >= numChars) | |
{ | |
curChar = 0; | |
try | |
{ | |
numChars = stream.read(buf); | |
} catch (IOException e) | |
{ | |
throw new InputMismatchException(); | |
} | |
if (numChars <= 0) | |
return -1; | |
} | |
return buf[curChar++]; | |
} | |
public int readInt() | |
{ | |
int c = read(); | |
while (isSpaceChar(c)) | |
c = read(); | |
int sgn = 1; | |
if (c == '-') | |
{ | |
sgn = -1; | |
c = read(); | |
} | |
int res = 0; | |
do | |
{ | |
if (c < '0' || c > '9') | |
throw new InputMismatchException(); | |
res *= 10; | |
res += c - '0'; | |
c = read(); | |
} while (!isSpaceChar(c)); | |
return res * sgn; | |
} | |
public String readString() | |
{ | |
int c = read(); | |
while (isSpaceChar(c)) | |
c = read(); | |
StringBuilder res = new StringBuilder(); | |
do | |
{ | |
res.appendCodePoint(c); | |
c = read(); | |
} while (!isSpaceChar(c)); | |
return res.toString(); | |
} | |
public double readDouble() { | |
int c = read(); | |
while (isSpaceChar(c)) | |
c = read(); | |
int sgn = 1; | |
if (c == '-') { | |
sgn = -1; | |
c = read(); | |
} | |
double res = 0; | |
while (!isSpaceChar(c) && c != '.') { | |
if (c == 'e' || c == 'E') | |
return res * Math.pow(10, readInt()); | |
if (c < '0' || c > '9') | |
throw new InputMismatchException(); | |
res *= 10; | |
res += c - '0'; | |
c = read(); | |
} | |
if (c == '.') { | |
c = read(); | |
double m = 1; | |
while (!isSpaceChar(c)) { | |
if (c == 'e' || c == 'E') | |
return res * Math.pow(10, readInt()); | |
if (c < '0' || c > '9') | |
throw new InputMismatchException(); | |
m /= 10; | |
res += (c - '0') * m; | |
c = read(); | |
} | |
} | |
return res * sgn; | |
} | |
public long readLong() { | |
int c = read(); | |
while (isSpaceChar(c)) | |
c = read(); | |
int sgn = 1; | |
if (c == '-') { | |
sgn = -1; | |
c = read(); | |
} | |
long res = 0; | |
do { | |
if (c < '0' || c > '9') | |
throw new InputMismatchException(); | |
res *= 10; | |
res += c - '0'; | |
c = read(); | |
} while (!isSpaceChar(c)); | |
return res * sgn; | |
} | |
public boolean isSpaceChar(int c) | |
{ | |
if (filter != null) | |
return filter.isSpaceChar(c); | |
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; | |
} | |
public String next() | |
{ | |
return readString(); | |
} | |
public interface SpaceCharFilter | |
{ | |
public boolean isSpaceChar(int ch); | |
} | |
} | |
private static class OutputWriter | |
{ | |
private final PrintWriter writer; | |
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(); | |
} | |
} | |
/* USAGE | |
//initialize | |
InputReader in = new InputReader(System.in); | |
OutputWriter out = new OutputWriter(System.out); | |
//read int | |
int i = in.readInt(); | |
//read string | |
String s = in.readString(); | |
//read int array of size N | |
int[] x = IOUtils.readIntArray(in,N); | |
//printline | |
out.printLine("X"); | |
//flush output | |
out.flush(); | |
//remember to close the | |
//outputstream, at the end | |
out.close(); | |
*/ | |
/*class IOUtils { | |
public static int[] readIntArray(InputReader in, int size) { | |
int[] array = new int[size]; | |
for (int i = 0; i < size; i++) | |
array[i] = in.readInt(); | |
return array;*/ | |
} |
This file contains 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
// Working program using Reader Class | |
import java.io.DataInputStream; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.util.Scanner; | |
import java.util.StringTokenizer; | |
public class Main | |
{ | |
static class Reader | |
{ | |
final private int BUFFER_SIZE = 1 << 16; | |
private DataInputStream din; | |
private byte[] buffer; | |
private int bufferPointer, bytesRead; | |
public Reader() | |
{ | |
din = new DataInputStream(System.in); | |
buffer = new byte[BUFFER_SIZE]; | |
bufferPointer = bytesRead = 0; | |
} | |
public Reader(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(); | |
} | |
} | |
public static void main(String[] args) throws IOException | |
{ | |
Reader s=new Reader(); | |
int n = s.nextInt(); | |
int k = s.nextInt(); | |
int count=0; | |
while (n-- > 0) | |
{ | |
int x = s.nextInt(); | |
if (x%k == 0) | |
count++; | |
} | |
System.out.println(count); | |
} | |
} |
This file contains 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.DataInputStream; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
public class Reader { | |
final private int BUFFER_SIZE = 1 << 16; | |
private DataInputStream din; | |
private byte[] buffer; | |
private int bufferPointer, bytesRead; | |
public Reader() { | |
din = new DataInputStream(System.in); | |
buffer = new byte[BUFFER_SIZE]; | |
bufferPointer = bytesRead = 0; | |
} | |
public Reader(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) { | |
buf[cnt++] = (byte) c; | |
if (c == '\n') | |
break; | |
} | |
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'; | |
c = read(); | |
} while (c >= '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'; | |
c = read(); | |
} while (c >= '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'; | |
c = read(); | |
} while (c >= '0' && c <= '9'); | |
if (c == '.') | |
while ((c = read()) >= '0' && c <= '9') { | |
div *= 10; | |
ret = ret + (c - '0') / div; | |
} | |
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(); | |
} | |
} |
This file contains 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.util.*; | |
import java.io.*; | |
class | |
{ | |
/************************ SOLUTION STARTS HERE ************************/ | |
private static void solve(FastScanner s1, PrintWriter out){ | |
/* | |
Start typing your solution here | |
get input by using "s1" object (It works exactly like Scanner but faster than it) | |
(eg). | |
int a = s1.nextInt() | |
String s = s1.nextLine() //Returns an entire line | |
String s = s1.next() //Returns only one word | |
Print output using "out" object | |
(eg). | |
out.println("Hello,World!") | |
*/ | |
} | |
/************************ SOLUTION ENDS HERE ************************/ | |
/************************ TEMPLATE STARTS HERE ************************/ | |
public static void main(String []args) throws IOException { | |
FastScanner in = new FastScanner(System.in); | |
PrintWriter out = | |
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)), false); | |
solve(in, out); | |
in.close(); | |
out.close(); | |
} | |
static class FastScanner{ | |
public BufferedReader reader; | |
public StringTokenizer st; | |
public FastScanner(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(); | |
} | |
public String nextLine(){ | |
String str = null; | |
try { | |
str = reader.readLine(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return str; | |
} | |
public int nextInt(){ | |
return Integer.parseInt(next()); | |
} | |
public long nextLong(){ | |
return Long.parseLong(next()); | |
} | |
public double nextDouble(){ | |
return Double.parseDouble(next()); | |
} | |
public char nextChar(){ | |
return next().charAt(0); | |
} | |
int[] nextIntArray(int n) { | |
int[] arr = new int[n]; | |
for (int i = 0; i < n; i++) { | |
arr[i] = nextInt(); | |
} | |
return arr; | |
} | |
long[] nextLongArray(int n) { | |
long[] arr = new long[n]; | |
for (int i = 0; i < n; i++) { | |
arr[i] = nextLong(); | |
} | |
return arr; | |
} | |
public void close(){ | |
try{ reader.close(); } catch(IOException e){e.printStackTrace();} | |
} | |
} | |
/************************ TEMPLATE ENDS HERE ************************/ | |
} |
This file contains 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
public class InputReader { | |
private InputStream stream; | |
private byte[] buf = new byte[1024]; | |
private int curChar; | |
private int numChars; | |
public InputReader(InputStream stream) { | |
this.stream = stream; | |
} | |
public int read() { | |
if (numChars == -1) | |
throw new RuntimeException(); | |
if (curChar >= numChars) { | |
curChar = 0; | |
try { | |
numChars = stream.read(buf); | |
} catch (IOException e) { | |
throw new RuntimeException(); | |
} | |
if (numChars <= 0) | |
return -1; | |
} | |
return buf[curChar++]; | |
} | |
public String readString() { | |
final StringBuilder stringBuilder = new StringBuilder(); | |
int c = read(); | |
while (isSpaceChar(c)) | |
c = read(); | |
do { | |
stringBuilder.append((char)c); | |
c = read(); | |
} while (!isSpaceChar(c)); | |
return stringBuilder.toString(); | |
} | |
public int readInt() { | |
int c = read(); | |
while (isSpaceChar(c)) | |
c = read(); | |
int sgn = 1; | |
if (c == '-') { | |
sgn = -1; | |
c = read(); | |
} | |
int res = 0; | |
do { | |
res *= 10; | |
res += c - '0'; | |
c = read(); | |
} while (!isSpaceChar(c)); | |
return res * sgn; | |
} | |
public long readLong() { | |
int c = read(); | |
while (isSpaceChar(c)) | |
c = read(); | |
int sgn = 1; | |
if (c == '-') { | |
sgn = -1; | |
c = read(); | |
} | |
long res = 0; | |
do { | |
res *= 10; | |
res += c - '0'; | |
c = read(); | |
} while (!isSpaceChar(c)); | |
return res * sgn; | |
} | |
public boolean isSpaceChar(int c) { | |
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; | |
} | |
} |
This file contains 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.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.util.StringTokenizer; | |
/** Class for buffered reading int and double values */ | |
public class FastReader { | |
public static BufferedReader reader; | |
public static StringTokenizer tokenizer; | |
/** call this method to initialize reader for InputStream */ | |
public static void init(InputStream input) { | |
reader = new BufferedReader(new InputStreamReader(input) ); | |
tokenizer = new StringTokenizer(""); | |
} | |
/** get next word */ | |
public static String next() throws IOException { | |
if (reader == null || tokenizer == null) { | |
throw new IOException("init method must be used before using the Reader."); | |
} | |
while ( ! tokenizer.hasMoreTokens() ) { | |
//TODO add check for eof if necessary | |
tokenizer = new StringTokenizer(reader.readLine() ); | |
} | |
return tokenizer.nextToken(); | |
} | |
public static int nextInt() throws NumberFormatException { | |
try { | |
return Integer.parseInt( next() ); | |
} catch (IOException e) { | |
throw new NumberFormatException(e.getMessage()); | |
} | |
} | |
public static double nextDouble() throws NumberFormatException { | |
try { | |
return Double.parseDouble( next() ); | |
} catch (IOException e) { | |
throw new NumberFormatException(e.getMessage()); | |
} | |
} | |
public static void close() { | |
try { | |
reader.close(); | |
} catch (IOException e) { | |
throw new IllegalStateException(); | |
} | |
} | |
} |
This file contains 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.InputStream; | |
import java.util.InputMismatchException; | |
import java.io.IOException; | |
public class Scan | |
{ | |
private byte[] buf = new byte[1024]; | |
private int total; | |
private int index; | |
private InputStream in; | |
public Scan() | |
{ | |
in = System.in; | |
} | |
public int scan() throws IOException | |
{ | |
if(total < 0) | |
throw new InputMismatchException(); | |
if(index >= total) | |
{ | |
index = 0; | |
total = in.read(buf); | |
if(total <= 0) | |
return -1; | |
} | |
return buf[index++]; | |
} | |
public int scanInt() throws IOException | |
{ | |
int integer = 0; | |
int n = scan(); | |
while(isWhiteSpace(n)) /* remove starting white spaces */ | |
n = scan(); | |
int neg = 1; | |
if(n == '-') | |
{ | |
neg = -1; | |
n = scan(); | |
} | |
while(!isWhiteSpace(n)) | |
{ | |
if(n >= '0' && n <= '9') | |
{ | |
integer *= 10; | |
integer += n-'0'; | |
n = scan(); | |
} | |
else | |
throw new InputMismatchException(); | |
} | |
return neg*integer; | |
} | |
public String scanString()throws IOException | |
{ | |
StringBuilder sb = new StringBuilder(); | |
int n = scan(); | |
while(isWhiteSpace(n)) | |
n = scan(); | |
while(!isWhiteSpace(n)) | |
{ | |
sb.append((char)n); | |
n = scan(); | |
} | |
return sb.toString(); | |
} | |
public double scanDouble()throws IOException | |
{ | |
double doub=0; | |
int n=scan(); | |
while(isWhiteSpace(n)) | |
n=scan(); | |
int neg=1; | |
if(n=='-') | |
{ | |
neg=-1; | |
n=scan(); | |
} | |
while(!isWhiteSpace(n)&& n != '.') | |
{ | |
if(n>='0'&&n<='9') | |
{ | |
doub*=10; | |
doub+=n-'0'; | |
n=scan(); | |
} | |
else throw new InputMismatchException(); | |
} | |
if(n=='.') | |
{ | |
n=scan(); | |
double temp=1; | |
while(!isWhiteSpace(n)) | |
{ | |
if(n>='0'&&n<='9') | |
{ | |
temp/=10; | |
doub+=(n-'0')*temp; | |
n=scan(); | |
} | |
else throw new InputMismatchException(); | |
} | |
} | |
return doub*neg; | |
} | |
public boolean isWhiteSpace(int n) | |
{ | |
if(n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) | |
return true; | |
return false; | |
} | |
public void close()throws IOException | |
{ | |
in.close(); | |
} | |
} |
This file contains 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.*; | |
import java.util.*; | |
public class Main{ | |
public static void main(String[] args) { | |
// Note: Do not pass System.in to the MyScanner() constructor | |
MyScanner sc = new MyScanner(); | |
int n = sc.nextInt(); // read input as integer | |
long k = sc.nextLong(); // read input as long | |
double d = sc.nextDouble(); // read input as double | |
String str = sc.next(); // read input as String | |
String s = sc.nextLine(); // read whole line as String | |
} | |
//-----------MyScanner class for faster input---------- | |
public static class MyScanner { | |
BufferedReader br; | |
StringTokenizer st; | |
public MyScanner() { | |
br = new BufferedReader(new InputStreamReader(System.in)); | |
} | |
String next() { | |
while (st == null || !st.hasMoreElements()) { | |
try { | |
st = new StringTokenizer(br.readLine()); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
return st.nextToken(); | |
} | |
int nextInt() { | |
return Integer.parseInt(next()); | |
} | |
long nextLong() { | |
return Long.parseLong(next()); | |
} | |
double nextDouble() { | |
return Double.parseDouble(next()); | |
} | |
String nextLine(){ | |
String str = ""; | |
try { | |
str = br.readLine(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return str; | |
} | |
} | |
//-------------------------------------------------------- | |
} |
This file contains 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
static class Reader{ | |
BufferedReader br; | |
StringTokenizer st; | |
public Reader() { | |
br = new BufferedReader(new InputStreamReader(System.in)); | |
} | |
String next() { | |
while(st != null && !st.hasMoreTokens()) { | |
st = new StringTokenizer(nextLine()); | |
} | |
return st.nextToken(); | |
} | |
int nextInt() { | |
return Integer.parseInt(next()); | |
} | |
double nextDouble() { | |
return Double.parseDouble(next()); | |
} | |
long nextLong() | |
{ | |
return Long.parseLong(next()); | |
} | |
String nextLine() { | |
String str = ""; | |
try { | |
str = br.readLine(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
return str; | |
} | |
} |
This file contains 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.IOException; | |
import java.io.InputStream; | |
import java.util.InputMismatchException; | |
class InputReader { | |
private InputStream stream; | |
private byte[] buf = new byte[102400]; | |
private int curChar; | |
private int numChars; | |
public InputReader(InputStream stream) { | |
this.stream = stream; | |
} | |
public final int read() { | |
if (numChars == -1) | |
throw new InputMismatchException(); | |
if (curChar >= numChars) { | |
curChar = 0; | |
try { | |
numChars = stream.read(buf); | |
} catch (IOException e) { | |
throw new InputMismatchException(); | |
} | |
if (numChars <= 0) | |
return -1; | |
} | |
return buf[curChar++]; | |
} | |
public final int readInt() { | |
int c = read(); | |
while (isSpaceChar(c)) | |
c = read(); | |
int sgn = 1; | |
if (c == '-') { | |
sgn = -1; | |
c = read(); | |
} | |
int res = 0; | |
do { | |
if (c < '0' || c > '9') | |
throw new InputMismatchException(); | |
res *= 10; | |
res += c - '0'; | |
c = read(); | |
} while (!isSpaceChar(c)); | |
return res * sgn; | |
} | |
public final long readLong() { | |
int c = read(); | |
while (isSpaceChar(c)) | |
c = read(); | |
int sgn = 1; | |
if (c == '-') { | |
sgn = -1; | |
c = read(); | |
} | |
long res = 0; | |
do { | |
if (c < '0' || c > '9') | |
throw new InputMismatchException(); | |
res *= 10; | |
res += c - '0'; | |
c = read(); | |
} while (!isSpaceChar(c)); | |
return res * sgn; | |
} | |
public final String readString() { | |
int c = read(); | |
while (isSpaceChar(c)) | |
c = read(); | |
StringBuilder res = new StringBuilder(); | |
do { | |
res.appendCodePoint(c); | |
c = read(); | |
} while (!isSpaceChar(c)); | |
return res.toString(); | |
} | |
public static boolean isSpaceChar(int c) { | |
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; | |
} | |
public final String next() { | |
return readString(); | |
} | |
} |
This file contains 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.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.util.*; | |
import java.io.*; | |
class FasterScanner { | |
private InputStream mIs; | |
private byte[] buf = new byte[1024]; | |
private int curChar; | |
private int numChars; | |
public FasterScanner() { | |
this(System.in); | |
} | |
public FasterScanner(InputStream is) { | |
mIs = is; | |
} | |
public int read() { | |
if (numChars == -1) | |
throw new InputMismatchException(); | |
if (curChar >= numChars) { | |
curChar = 0; | |
try { | |
numChars = mIs.read(buf); | |
} catch (IOException e) { | |
throw new InputMismatchException(); | |
} | |
if (numChars <= 0) | |
return -1; | |
} | |
return buf[curChar++]; | |
} | |
public String nextLine() { | |
int c = read(); | |
while (isSpaceChar(c)) | |
c = read(); | |
StringBuilder res = new StringBuilder(); | |
do { | |
res.appendCodePoint(c); | |
c = read(); | |
} while (!isEndOfLine(c)); | |
return res.toString(); | |
} | |
public String nextString() { | |
int c = read(); | |
while (isSpaceChar(c)) | |
c = read(); | |
StringBuilder res = new StringBuilder(); | |
do { | |
res.appendCodePoint(c); | |
c = read(); | |
} while (!isSpaceChar(c)); | |
return res.toString(); | |
} | |
public long nextLong() { | |
int c = read(); | |
while (isSpaceChar(c)) | |
c = read(); | |
int sgn = 1; | |
if (c == '-') { | |
sgn = -1; | |
c = read(); | |
} | |
long res = 0; | |
do { | |
if (c < '0' || c > '9') | |
throw new InputMismatchException(); | |
res *= 10; | |
res += c - '0'; | |
c = read(); | |
} while (!isSpaceChar(c)); | |
return res * sgn; | |
} | |
public int nextInt() { | |
int c = read(); | |
while (isSpaceChar(c)) | |
c = read(); | |
int sgn = 1; | |
if (c == '-') { | |
sgn = -1; | |
c = read(); | |
} | |
int res = 0; | |
do { | |
if (c < '0' || c > '9') | |
throw new InputMismatchException(); | |
res *= 10; | |
res += c - '0'; | |
c = read(); | |
} while (!isSpaceChar(c)); | |
return res * sgn; | |
} | |
public boolean isSpaceChar(int c) { | |
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; | |
} | |
public boolean isEndOfLine(int c) { | |
return c == '\n' || c == '\r' || c == -1; | |
} | |
} |
This file contains 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
static class FastReader { | |
BufferedReader br; | |
StringTokenizer st; | |
public FastReader() throws Exception { | |
br = new BufferedReader(new InputStreamReader(System.in)); | |
} | |
String next() { | |
while (st == null || !st.hasMoreElements()) { | |
try { | |
st = new StringTokenizer(br.readLine()); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
return st.nextToken(); | |
} | |
int nextInt() { | |
return Integer.parseInt(next()); | |
} | |
long nextLong() { | |
return Long.parseLong(next()); | |
} | |
double nextDouble() { | |
return Double.parseDouble(next()); | |
} | |
String nextLine() { | |
String str = ""; | |
try { | |
str = br.readLine(); | |
} | |
catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return str; | |
} | |
} |
This file contains 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.FileInputStream; | |
import java.io.DataInputStream; | |
import java.io.IOException; | |
public class FastReader{ | |
final private int BUFFER_SIZE = 1 << 16; | |
private DataInputStream din; | |
private byte[] buffer; | |
private int bufferPointer, bytesRead; | |
public FastReader(){ | |
din = new DataInputStream(System.in); | |
buffer = new byte[BUFFER_SIZE]; | |
bufferPointer = bytesRead = 0; | |
} | |
public FastReader(String file_name){ | |
try{ | |
din = new DataInputStream(new FileInputStream(file_name)); | |
buffer = new byte[BUFFER_SIZE]; | |
bufferPointer = bytesRead = 0; | |
} | |
catch(IOException e){ | |
e.printStackTrace(); | |
} | |
} | |
public String readLine(){ | |
StringBuilder sb = new StringBuilder(); | |
byte c; | |
while((c = read()) != -1){ | |
if(c == '\n'){ | |
break; | |
} | |
sb.append((char)c); | |
} | |
return sb.toString(); | |
} | |
public String readLine(int size){ | |
byte[] buf = new byte[size]; | |
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(){ | |
int n = 0; | |
boolean neg = false; | |
int c; | |
while((c = read()) <= ' '); | |
neg = c == '-'; | |
if(neg){ | |
c = read(); | |
} | |
do{ | |
n = n * 10 + c - '0'; | |
} | |
while((c = read()) >= '0' && c <= '9'); | |
if(c == 13){ | |
read(); | |
} | |
return neg ? -n : n; | |
} | |
public long nextLong(){ | |
long n = 0; | |
boolean neg = false; | |
int c; | |
while((c = read()) <= ' '); | |
neg = c == '-'; | |
if(neg){ | |
c = read(); | |
} | |
do{ | |
n = n * 10 + c - '0'; | |
} | |
while((c = read()) >= '0' && c <= '9'); | |
if(c == 13){ | |
read(); | |
} | |
return neg ? -n : n; | |
} | |
public double nextDouble(){ | |
double n = 0, div = 1; | |
boolean neg = false; | |
int c; | |
while((c = read()) <= ' '); | |
neg = c == '-'; | |
if(neg){ | |
c = read(); | |
} | |
do{ | |
n = n * 10 + c - '0'; | |
} | |
while((c = read()) >= '0' && c <= '9'); | |
if(c == '.'){ | |
while((c = read()) >= '0' && c <= '9'){ | |
n += (c - '0') / (div *= 10); | |
} | |
} | |
if(c == 13){ | |
read(); | |
} | |
return neg ? -n : n; | |
} | |
private void fillBuffer(){ | |
try{ | |
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); | |
if(bytesRead == -1){ | |
buffer[0] = -1; | |
} | |
} | |
catch(IOException e){ | |
e.printStackTrace(); | |
} | |
} | |
private byte read(){ | |
if(bufferPointer == bytesRead){ | |
fillBuffer(); | |
} | |
return buffer[bufferPointer++]; | |
} | |
public void close(){ | |
try{ | |
if(din == null){ | |
return; | |
} | |
else{ | |
din.close(); | |
} | |
} | |
catch(IOException e){ | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains 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.*; | |
import java.util.StringTokenizer; | |
public class MyClass { | |
public static void main(String [] args) throws IOException { | |
Scanner sc = new Scanner(System.in); | |
OutputWriter out = new OutputWriter(System.out); | |
int in = sc.nextInt(); | |
// If you don't use the \n the writer will print trash | |
// The fast input should be enough | |
out.print(in + "\n"); | |
out.close(); | |
} | |
} | |
class Scanner{ | |
public BufferedReader reader; | |
public StringTokenizer st; | |
public Scanner(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(); | |
} | |
public int nextInt(){ | |
return Integer.parseInt(next()); | |
} | |
public long nextLong(){ | |
return Long.parseLong(next()); | |
} | |
public double nextDouble(){ | |
return Double.parseDouble(next()); | |
} | |
} | |
class OutputWriter{ | |
BufferedWriter writer; | |
public OutputWriter(OutputStream stream){ | |
writer = new BufferedWriter(new OutputStreamWriter(stream)); | |
} | |
public void print(int i) throws IOException { | |
writer.write(i); | |
} | |
public void print(String s) throws IOException { | |
writer.write(s); | |
} | |
public void print(char []c) throws IOException { | |
writer.write(c); | |
} | |
public void close() throws IOException { | |
writer.close(); | |
} | |
} |
This file contains 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.*; | |
/** | |
* @author Minho Kim <minho.kim093@gmail.com> | |
*/ | |
public class FastIO { | |
public static final int DEFAULT_BUFFER_SIZE = 65536; | |
public static final int DEFAULT_INTEGER_SIZE = 10; | |
public static final int DEFAULT_LONG_SIZE = 19; | |
public static final int DEFAULT_WORD_SIZE = 32; | |
public static final int DEFAULT_LINE_SIZE = 256; | |
public static final int EOF = -1; | |
private final InputStream in; | |
private final OutputStream out; | |
private byte[] inBuffer; | |
private int nextIn, inLength; | |
private byte[] outBuffer; | |
private int nextOut; | |
private char[] charBuffer; | |
private byte[] byteBuffer; | |
public FastIO(InputStream in, OutputStream out, int inBufferSize, int outBufferSize) { | |
this.in = in; | |
this.inBuffer = new byte[inBufferSize]; | |
this.nextIn = 0; | |
this.inLength = 0; | |
this.out = out; | |
this.outBuffer = new byte[outBufferSize]; | |
this.nextOut = 0; | |
this.charBuffer = new char[DEFAULT_LINE_SIZE]; | |
this.byteBuffer = new byte[DEFAULT_LONG_SIZE]; | |
} | |
public FastIO(InputStream in, OutputStream out) { | |
this(in, out, DEFAULT_BUFFER_SIZE, DEFAULT_BUFFER_SIZE); | |
} | |
public FastIO(InputStream in, OutputStream out, int bufferSize) { | |
this(in, out, bufferSize, bufferSize); | |
} | |
public char nextChar() { | |
return (char) read(); | |
} | |
public String next() { | |
byte b; | |
while (isSpace(b = read())) | |
; | |
int pos = 0; | |
do { | |
charBuffer[pos++] = (char) b; | |
} while (!isSpace(b = read())); | |
return new String(charBuffer, 0, pos); | |
} | |
public String nextLine() { | |
byte b; | |
int pos = 0; | |
while (!isLine(b = read())) | |
charBuffer[pos++] = (char) b; | |
return new String(charBuffer, 0, pos); | |
} | |
public int nextInt() { | |
byte b; | |
while (isSpace(b = read())) | |
; | |
boolean negative = false; | |
int result = b - '0'; | |
if (b == '-') { | |
negative = true; | |
result = 0; | |
} | |
while (isDigit(b = read())) | |
result = (result * 10) + (b - '0'); | |
return negative ? -result : result; | |
} | |
public long nextLong() { | |
byte b; | |
while (isSpace(b = read())) | |
; | |
boolean negative = false; | |
long result = b - '0'; | |
if (b == '-') { | |
negative = true; | |
result = 0; | |
} | |
while (isDigit(b = read())) | |
result = (result * 10) + (b - '0'); | |
return negative ? -result : result; | |
} | |
public float nextFloat() { | |
byte b; | |
while (isSpace(b = read())) | |
; | |
int pos = 0; | |
do { | |
charBuffer[pos++] = (char) b; | |
} while (!isSpace(b = read())); | |
return Float.parseFloat(new String(charBuffer, 0, pos)); | |
} | |
public float nextFloat2() { | |
byte b; | |
while (isSpace(b = read())) | |
; | |
boolean negative = false; | |
float result = b - '0'; | |
if (b == '-') { | |
negative = true; | |
result = 0; | |
} | |
while (isDigit(b = read())) | |
result = (result * 10) + (b - '0'); | |
float d = 1; | |
if (b == '.') { | |
while (isDigit(b = read())) | |
result += (b - '0') / (d *= 10); | |
} | |
return negative ? -result : result; | |
} | |
public double nextDouble() { | |
byte b; | |
while (isSpace(b = read())) | |
; | |
int pos = 0; | |
do { | |
charBuffer[pos++] = (char) b; | |
} while (!isSpace(b = read())); | |
return Double.parseDouble(new String(charBuffer, 0, pos)); | |
} | |
public double nextDouble2() { | |
byte b; | |
while (isSpace(b = read())) | |
; | |
boolean negative = false; | |
double result = b - '0'; | |
if (b == '-') { | |
negative = true; | |
result = 0; | |
} | |
while (isDigit(b = read())) | |
result = (result * 10) + (b - '0'); | |
double d = 1; | |
if (b == '.') { | |
while (isDigit(b = read())) | |
result += (b - '0') / (d *= 10); | |
} | |
return negative ? -result : result; | |
} | |
public int[] nextIntArray(int size) { | |
int[] array = new int[size]; | |
for (int i = 0; i < size; i++) | |
array[i] = nextInt(); | |
return array; | |
} | |
public long[] nextLongArray(int size) { | |
long[] array = new long[size]; | |
for (int i = 0; i < size; i++) | |
array[i] = nextLong(); | |
return array; | |
} | |
public int[][] nextInt2DArray(int Y, int X) { | |
int[][] array = new int[Y][X]; | |
for (int y = 0; y < Y; y++) | |
for (int x = 0; x < X; x++) | |
array[y][x] = nextInt(); | |
return array; | |
} | |
public void print(char c) { | |
write((byte) c); | |
} | |
public void print(String s) { | |
for (int i = 0; i < s.length(); i++) | |
write((byte) s.charAt(i)); | |
} | |
public void print(int i) { | |
if (i == 0) { | |
write((byte) '0'); | |
return; | |
} | |
if (i == Integer.MIN_VALUE) { | |
write((byte) '-'); | |
write((byte) '2'); | |
write((byte) '1'); | |
write((byte) '4'); | |
write((byte) '7'); | |
write((byte) '4'); | |
write((byte) '8'); | |
write((byte) '3'); | |
write((byte) '6'); | |
write((byte) '4'); | |
write((byte) '8'); | |
return; | |
} | |
if (i < 0) { | |
write((byte) '-'); | |
i = -i; | |
} | |
int pos = 0; | |
while (i > 0) { | |
byteBuffer[pos++] = (byte) ((i % 10) + '0'); | |
i /= 10; | |
} | |
while (pos-- > 0) | |
write(byteBuffer[pos]); | |
} | |
public void print(long l) { | |
if (l == 0) { | |
write((byte) '0'); | |
return; | |
} | |
if (l == Long.MIN_VALUE) { | |
write((byte) '-'); | |
write((byte) '9'); | |
write((byte) '2'); | |
write((byte) '2'); | |
write((byte) '3'); | |
write((byte) '3'); | |
write((byte) '7'); | |
write((byte) '2'); | |
write((byte) '0'); | |
write((byte) '3'); | |
write((byte) '6'); | |
write((byte) '8'); | |
write((byte) '5'); | |
write((byte) '4'); | |
write((byte) '7'); | |
write((byte) '7'); | |
write((byte) '5'); | |
write((byte) '8'); | |
write((byte) '0'); | |
write((byte) '8'); | |
return; | |
} | |
if (l < 0) { | |
write((byte) '-'); | |
l = -l; | |
} | |
int pos = 0; | |
while (l > 0) { | |
byteBuffer[pos++] = (byte) ((l % 10) + '0'); | |
l /= 10; | |
} | |
while (pos-- > 0) | |
write(byteBuffer[pos]); | |
} | |
public void print(float f) { | |
String sf = Float.toString(f); | |
for (int i = 0; i < sf.length(); i++) | |
write((byte) sf.charAt(i)); | |
} | |
public void print(double d) { | |
String sd = Double.toString(d); | |
for (int i = 0; i < sd.length(); i++) | |
write((byte) sd.charAt(i)); | |
} | |
public void println(char c) { | |
print(c); | |
write((byte) '\n'); | |
} | |
public void println(String s) { | |
print(s); | |
write((byte) '\n'); | |
} | |
public void println(int i) { | |
print(i); | |
write((byte) '\n'); | |
} | |
public void println(long l) { | |
print(l); | |
write((byte) '\n'); | |
} | |
public void println(float f) { | |
print(f); | |
write((byte) '\n'); | |
} | |
public void println(double d) { | |
print(d); | |
write((byte) '\n'); | |
} | |
public void printf(String format, Object... args) { | |
String s = String.format(format, args); | |
for (int i = 0; i < s.length(); i++) | |
write((byte) s.charAt(i)); | |
} | |
public void fprint(char c) { | |
print(c); | |
flushBuffer(); | |
} | |
public void fprint(String s) { | |
print(s); | |
flushBuffer(); | |
} | |
public void fprint(int i) { | |
print(i); | |
flushBuffer(); | |
} | |
public void fprint(long l) { | |
print(l); | |
flushBuffer(); | |
} | |
public void fprint(float f) { | |
print(f); | |
flushBuffer(); | |
} | |
public void fprint(double d) { | |
print(d); | |
flushBuffer(); | |
} | |
public void fprintf(String format, Object... args) { | |
printf(format, args); | |
flushBuffer(); | |
} | |
private byte read() { | |
if (nextIn >= inLength) { | |
if ((inLength = fill()) == EOF) | |
return EOF; | |
nextIn = 0; | |
} | |
return inBuffer[nextIn++]; | |
} | |
private void write(byte b) { | |
if (nextOut >= outBuffer.length) | |
flushBuffer(); | |
outBuffer[nextOut++] = b; | |
} | |
private int fill() { | |
try { | |
return in.read(inBuffer, 0, inBuffer.length); | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
public void flush() { | |
if (nextOut == 0) | |
return; | |
flushBuffer(); | |
} | |
private void flushBuffer() { | |
try { | |
out.write(outBuffer, 0, nextOut); | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
nextOut = 0; | |
} | |
public void close() { | |
flush(); | |
} | |
private boolean isDigit(byte b) { | |
return b >= '0' && b <= '9'; | |
} | |
private boolean isLine(byte b) { | |
return b == '\n' || b == '\r' || b == EOF; | |
} | |
private boolean isSpace(byte b) { | |
return b == ' ' || b == '\t' || b == '\n' || b == '\r' || b == '\f' || b == EOF; | |
} | |
} |
This file contains 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.*; | |
import java.util.*; | |
public class Main{ | |
public static void main(String[] args) { | |
MyScanner sc = new MyScanner(); | |
out = new PrintWriter(new BufferedOutputStream(System.out), true); | |
// Start writing your solution here. ------------------------------------- | |
int cnt = 0; | |
int n = sc.nextInt(); | |
int k = sc.nextInt(); | |
int t; | |
for(int i=0; i<n; i++){ | |
t = sc.nextInt(); | |
if(t % k == 0) cnt++; | |
} | |
out.println(cnt); | |
/* | |
int n = sc.nextInt(); // read input as integer | |
long k = sc.nextLong(); // read input as long | |
double d = sc.nextDouble(); // read input as double | |
String str = sc.next(); // read input as String | |
String s = sc.nextLine(); // read whole line as String | |
int result = 3*n; | |
out.println(result); // print via PrintWriter | |
*/ | |
// Stop writing your solution here. ------------------------------------- | |
out.close(); | |
} | |
//-----------PrintWriter for faster output--------------------------------- | |
public static PrintWriter out; | |
//-----------MyScanner class for faster input---------- | |
public static class MyScanner { | |
BufferedReader br; | |
StringTokenizer st; | |
public MyScanner() { | |
br = new BufferedReader(new InputStreamReader(System.in)); | |
} | |
String next() { | |
while (st == null || !st.hasMoreElements()) { | |
try { | |
st = new StringTokenizer(br.readLine()); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
return st.nextToken(); | |
} | |
int nextInt() { return Integer.parseInt(next()); } | |
long nextLong() { return Long.parseLong(next()); } | |
double nextDouble() { return Double.parseDouble(next()); } | |
String nextLine(){ | |
String str = ""; | |
try { | |
str = br.readLine(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return str; | |
} | |
} | |
//-------------------------------------------------------- | |
} |
This file contains 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
class Scan | |
{ | |
private byte[] buf=new byte[1024]; | |
private int index; | |
private InputStream in; | |
private int total; | |
public Scan() | |
{ | |
in=System.in; | |
} | |
public int scan()throws IOException | |
{ | |
if(total<0) | |
throw new InputMismatchException(); | |
if(index>=total) | |
{ | |
index=0; | |
total=in.read(buf); | |
if(total<=0) | |
return -1; | |
} | |
return buf[index++]; | |
} | |
public int scanInt()throws IOException | |
{ | |
int integer=0; | |
int n=scan(); | |
while(isWhiteSpace(n)) | |
n=scan(); | |
int neg=1; | |
if(n=='-') | |
{ | |
neg=-1; | |
n=scan(); | |
} | |
while(!isWhiteSpace(n)) | |
{ | |
if(n>='0'&&n<='9') | |
{ | |
integer*=10; | |
integer+=n-'0'; | |
n=scan(); | |
} | |
else throw new InputMismatchException(); | |
} | |
return neg*integer; | |
} | |
public double scanDouble()throws IOException | |
{ | |
double doub=0; | |
int n=scan(); | |
while(isWhiteSpace(n)) | |
n=scan(); | |
int neg=1; | |
if(n=='-') | |
{ | |
neg=-1; | |
n=scan(); | |
} | |
while(!isWhiteSpace(n)&&n!='.') | |
{ | |
if(n>='0'&&n<='9') | |
{ | |
doub*=10; | |
doub+=n-'0'; | |
n=scan(); | |
} | |
else throw new InputMismatchException(); | |
} | |
if(n=='.') | |
{ | |
n=scan(); | |
double temp=1; | |
while(!isWhiteSpace(n)) | |
{ | |
if(n>='0'&&n<='9') | |
{ | |
temp/=10; | |
doub+=(n-'0')*temp; | |
n=scan(); | |
} | |
else throw new InputMismatchException(); | |
} | |
} | |
return doub*neg; | |
} | |
public String scanString()throws IOException | |
{ | |
StringBuilder sb=new StringBuilder(); | |
int n=scan(); | |
while(isWhiteSpace(n)) | |
n=scan(); | |
while(!isWhiteSpace(n)) | |
{ | |
sb.append((char)n); | |
n=scan(); | |
} | |
return sb.toString(); | |
} | |
private boolean isWhiteSpace(int n) | |
{ | |
if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1) | |
return true; | |
return false; | |
} | |
} |
This file contains 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
class InputReader { | |
private InputStream stream; | |
private byte[] buf = new byte[1024]; | |
private int curChar; | |
private int numChars; | |
private SpaceCharFilter filter; | |
public InputReader(InputStream stream) { | |
this.stream = stream; | |
} | |
public int read() { | |
if (numChars == -1) | |
throw new InputMismatchException(); | |
if (curChar >= numChars) { | |
curChar = 0; | |
try { | |
numChars = stream.read(buf); | |
} catch (IOException e) { | |
throw new InputMismatchException(); | |
} | |
if (numChars <= 0) | |
return -1; | |
} | |
return buf[curChar++]; | |
} | |
public int readInt() { | |
int c = read(); | |
while (isSpaceChar(c)) | |
c = read(); | |
int sgn = 1; | |
if (c == '-') { | |
sgn = -1; | |
c = read(); | |
} | |
int res = 0; | |
do { | |
if (c < '0' || c > '9') | |
throw new InputMismatchException(); | |
res *= 10; | |
res += c - '0'; | |
c = read(); | |
} while (!isSpaceChar(c)); | |
return res * sgn; | |
} | |
public String readString() { | |
int c = read(); | |
while (isSpaceChar(c)) | |
c = read(); | |
StringBuilder res = new StringBuilder(); | |
do { | |
res.appendCodePoint(c); | |
c = read(); | |
} while (!isSpaceChar(c)); | |
return res.toString(); | |
} | |
public boolean isSpaceChar(int c) { | |
if (filter != null) | |
return filter.isSpaceChar(c); | |
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; | |
} | |
public String next() { | |
return readString(); | |
} | |
public interface SpaceCharFilter { | |
public boolean isSpaceChar(int ch); | |
} | |
} | |
class OutputWriter { | |
private final PrintWriter writer; | |
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(); | |
} | |
} | |
class IOUtils { | |
public static int[] readIntArray(InputReader in , int size) { | |
int[] array = new int[size]; | |
for (int i = 0; i < size; i++) | |
array[i] = in .readInt(); | |
return array; | |
} | |
} | |
//Usges | |
//initialize | |
InputReader in = new InputReader(System.in); | |
OutputWriter out = new OutputWriter(System.out); | |
//read int | |
int i = in.readInt(); | |
//read string | |
String s = in.readString(); | |
//read int array of size N | |
int[] x = IOUtils.readIntArray(in,N); | |
//printline | |
out.printLine("X"); | |
//flush output | |
out.flush(); | |
//remember to close the | |
//outputstream, at the end | |
out.close(); |
This file contains 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
class Print | |
{ | |
private final BufferedWriter bw; | |
public Print() | |
{ | |
this.bw=new BufferedWriter(new OutputStreamWriter(System.out)); | |
} | |
public void print(Object object)throws IOException | |
{ | |
bw.append(""+object); | |
} | |
public void println(Object object)throws IOException | |
{ | |
print(object); | |
bw.append("\n"); | |
} | |
public void close()throws IOException | |
{ | |
bw.close(); | |
} | |
} |
This file contains 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.BufferedWriter; | |
import java.io.IOException; | |
import java.io.OutputStreamWriter; | |
public class Print | |
{ | |
private BufferedWriter bw; | |
public Print() | |
{ | |
this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); | |
} | |
public void print(Object object)throws IOException | |
{ | |
bw.append("" + object); | |
} | |
public void println(Object object)throws IOException | |
{ | |
print(object); | |
bw.append("\n"); | |
} | |
public void close()throws IOException | |
{ | |
bw.close(); | |
} | |
} |
This file contains 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.PrintWriter; | |
import java.io.File; | |
import java.io.IOException; | |
public class FastWriter{ | |
private PrintWriter pw; | |
public FastWriter(){ | |
pw = new PrintWriter(System.out); | |
} | |
public FastWriter(String file_name){ | |
try{ | |
pw = new PrintWriter(new File(file_name)); | |
} | |
catch(IOException e){ | |
e.printStackTrace(); | |
} | |
} | |
public void print(Object o){ | |
pw.print(o); | |
} | |
public void println(Object o){ | |
pw.println(o); | |
} | |
public void println(){ | |
pw.println(); | |
} | |
public void printIntArray(int arr[], String sep){ | |
for(int i = 0; i < arr.length - 1; i++){ | |
print(arr[i] + sep); | |
} | |
println(arr[arr.length - 1]); | |
} | |
public void close(){ | |
pw.close(); | |
} | |
public void flush(){ | |
pw.flush(); | |
} | |
} |
This file contains 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
/** | |
*1. Instead of using BufferedReader, use the following template of FastScanner. | |
*2. Instead of i<n in the for loop, use i!=n. | |
*3. Instead of count++ and i++, use ++count and ++i respectively. | |
*4.Instead of using Math.pow, write code using fast binary exponentiation. | |
*5. Do not use split(" "). | |
*/ | |
import java.util.*; | |
import Read about technology. - java.io.*; | |
public class Template { | |
FastScanner in ; | |
PrintWriter out; | |
public void solve() throws IOException {} | |
public void run() { | |
try { in = new FastScanner(new File(".in")); | |
out = new PrintWriter(new File(".out")); | |
solve(); | |
out.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
class FastScanner { | |
BufferedReader br; | |
StringTokenizer st; | |
FastScanner(File f) { | |
try { | |
br = new BufferedReader(new FileReader(f)); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} | |
} | |
String next() { | |
while (st == null || !st.hasMoreTokens()) { | |
try { | |
st = new StringTokenizer(br.readLine()); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
return st.nextToken(); | |
} | |
int nextInt() { | |
return Integer.parseInt(next()); | |
} | |
} | |
public static void main(String[] arg) { | |
new Template().run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment