12833번 XORXORXOR | 뭐라도 하겠지

12833번 XORXORXOR

이왜틀?


img01

12833번 XORXORXOR - 백준

당분간 틀린 브론즈 문제로 힐링해야지.
5년 전 시간초과다.

try (BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));) {
	StringTokenizer st=new StringTokenizer(br.readLine()," ");
	int a=Integer.parseInt(st.nextToken());
	int b=Integer.parseInt(st.nextToken());
	int c=Integer.parseInt(st.nextToken());
	for(int i=0;i<c;i++) a^=b;
	bw.write(a+"");
}catch(Exception e){e.printStackTrace();}

XOR 연산을 실제로 C번 진행해서 시간초과가 난다.

설계


C가 최대 10^9인데 제한시간이 0.2초라서 시뮬레이션하면 터진다.

XOR을 2번하면 원래대로 돌아오므로 C가 짝수면 A, 홀수면 A ^ B 를 출력한다.

구현


버퍼드리더로 충분할 것 같긴 한데 빠른 입력 만들어둔걸 또 가져와서 쓰자.

public static void main(String[] args) throws IOException{

	InputReader ir = new InputReader();
	
	int A = ir.nextInt();
	int B = ir.nextInt();
	int C = ir.nextInt();
	
	if (C % 2 == 0) System.out.println(A);
	else System.out.println(A ^ B);
}

// 빠른 입출력 구현
public static class InputReader {
	
	private final InputStream in = System.in;
	private final byte[] buf = new byte[1<<20];
	
	private int ptr = 0;
	private int buflen = 0;

	int nextInt() throws IOException {
		int c = 0;
		int x = 0;
		boolean negative = false;
		
		while(true) {
			c = read();
			if (c > ' ') break;
		}
		
		if (c == '-') { 
			negative = true;
			c = read(); 
		}
		
		while(c >= '0' && c <= '9') {
			x = x * 10 + (c - '0');
			c= read();
		}
		
		return negative ? -x : x;
	}

	private int read() throws IOException {
		if (ptr >= buflen) {
			buflen = in.read(buf);
			ptr = 0;
			if (buflen <= 0) {
				return -1;
			}
		}
		
		return buf[ptr++] & 0xFF;
	}
}

채점


img02

반성


뭔가 불만족스러운 속도는 출력에 신경을 쓰지 않아서인가…
빠른 출력 코드도 찾아와서 다음에 써먹어야겠다.
분명히 예전에 해둔게 있을건데

코드 확인


Link to GitHub

Updated: