Skip to content

Commit 9cf8e23

Browse files
authored
[20250306] BOJ / P5 / 분수 / 권혁준
1 parent 02948d1 commit 9cf8e23

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
```java
2+
3+
import java.util.*;
4+
import java.io.*;
5+
import java.math.BigInteger;
6+
7+
8+
class Main {
9+
10+
// IO field
11+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
static StringTokenizer st;
14+
15+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
16+
static int nextInt() {return Integer.parseInt(st.nextToken());}
17+
static long nextLong() {return Long.parseLong(st.nextToken());}
18+
static void bwEnd() throws Exception {bw.flush();bw.close();}
19+
20+
// Additional field
21+
22+
static long T, I, N;
23+
static String A, B;
24+
25+
public static void main(String[] args) throws Exception {
26+
27+
T = Integer.parseInt(br.readLine());
28+
while(T-- > 0) {
29+
30+
ready();
31+
solve();
32+
33+
}
34+
35+
bwEnd();
36+
37+
}
38+
39+
static void ready() throws Exception{
40+
41+
nextLine();
42+
A = st.nextToken();
43+
B = st.nextToken();
44+
45+
nextLine();
46+
I = nextLong();
47+
N = nextLong();
48+
49+
}
50+
51+
static void solve() throws Exception{
52+
53+
BigInteger a = new BigInteger(A);
54+
BigInteger b = new BigInteger(B);
55+
a = a.mod(b);
56+
57+
// a가 더 작으면
58+
BigInteger t = power(new BigInteger("10"), I-1, b);
59+
a = a.multiply(t).mod(b);
60+
for(int i=0;i<N;i++) {
61+
bw.write(a.multiply(new BigInteger("10")).divide(b).toString().charAt(0));
62+
a = a.multiply(new BigInteger("10")).mod(b);
63+
}
64+
bw.write("\n");
65+
66+
}
67+
68+
static BigInteger power(BigInteger x, long p, BigInteger m) {
69+
if(p == 0) return new BigInteger("1");
70+
if(p == 1) return x.mod(m);
71+
BigInteger half = power(x, p>>1, m).mod(m);
72+
half = half.multiply(half).mod(m);
73+
if(p%2 == 0) return half;
74+
return half.multiply(x).mod(m);
75+
}
76+
77+
}
78+
79+
```

0 commit comments

Comments
 (0)