Skip to content

Commit 53ded45

Browse files
authored
Merge pull request #309 from AlgorithmWithGod/khj20006
[20250415] SWEA / D4 / 팩토리얼과 최대공약수 / 권혁준
2 parents 3a731f3 + 19e3c40 commit 53ded45

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
```java
2+
3+
import java.util.*;
4+
5+
import java.io.*;
6+
7+
public class Solution {
8+
9+
// IO field
10+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
12+
static StringTokenizer st = new StringTokenizer("");
13+
14+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
15+
static String nextToken() throws Exception {
16+
while(!st.hasMoreTokens()) nextLine();
17+
return st.nextToken();
18+
}
19+
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
20+
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
21+
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
22+
static void bwEnd() throws Exception {bw.flush();bw.close();}
23+
24+
25+
// Additional field
26+
27+
static long N, K;
28+
29+
//
30+
31+
public static void main(String[] args) throws Exception {
32+
33+
int TC = nextInt();
34+
for(int tc=1;tc<=TC;tc++) {
35+
36+
ready();
37+
solve(tc);
38+
39+
}
40+
bwEnd();
41+
42+
}
43+
44+
static void ready() throws Exception {
45+
46+
N = nextInt();
47+
K = nextInt();
48+
49+
}
50+
51+
static void solve(int tc) throws Exception {
52+
bw.write("#" + tc + " ");
53+
54+
long ans = 1;
55+
for(long i=2;i*i<=K;i++) {
56+
long cnt = 0, d = 1;
57+
while(K%i == 0) {
58+
d *= i;
59+
K /= i;
60+
cnt++;
61+
}
62+
long ncnt = 0;
63+
for(long j=i;j<=N;j*=i) ncnt += N/j;
64+
while(cnt-ncnt>0) {
65+
cnt--;
66+
d /= i;
67+
}
68+
ans *= d;
69+
}
70+
if(N/K >= 1) ans *= K;
71+
bw.write(ans + "\n");
72+
73+
}
74+
75+
}
76+
77+
```

0 commit comments

Comments
 (0)