-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution_1493_수의새로운연산_유승아.java
89 lines (65 loc) · 1.74 KB
/
Solution_1493_수의새로운연산_유승아.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
//SWEA :: 1493 수의 새로운 연산
//2019-03-06
public class Solution_1493_수의새로운연산_유승아 {
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(bf.readLine());
start();
for (int tc = 1; tc <= T; tc++) {
StringTokenizer st = new StringTokenizer(bf.readLine(), " ");
long x = Long.parseLong(st.nextToken());
long y = Long.parseLong(st.nextToken());
long ans = cal(x, y);
System.out.println("#" + tc + " " + ans);
} // end of tc
}// end of main
public static long cal(long x, long y) {
pair p1 = r_sharp(x);
pair p2 = r_sharp(y);
long nx = p1.x + p2.x;
long ny = p1.y + p2.y;
long ret = sharp(nx, ny);
return ret;
}
static int[] Yfirst = new int[300];
public static void start() {
int ygap = 1;
Yfirst[1] = 1;
for (int i = 2; i < Yfirst.length; i++) {
Yfirst[i] = Yfirst[i - 1] + (ygap++);
}
}
public static long sharp(long x, long y) {
long cha = x - 1;
int yy = (int) (y + cha); // 시작점의 y좌표
int a1 = Yfirst[yy];
long ret = a1 + (x - 1);
return ret;
}
public static pair r_sharp(long p) {
int a1 = 0, idx = -1;
for (int i = 0; i < Yfirst.length; i++) {
if (Yfirst[i] > p) {
idx = i - 1;
a1 = Yfirst[i - 1];
break;
}
}
// 시작점 좌표는 (1,idx)
long cha = p - a1;
return new pair(1 + cha, idx - cha);
}
static class pair {
long x;
long y;
public pair(long x, long y) {
super();
this.x = x;
this.y = y;
}
}
}