Skip to content

Commit bcb0c78

Browse files
authored
Merge pull request #714 from AlgorithmWithGod/khj20006
[20250822] BOJ / P5 / (Relatively) Prime / 권혁준
2 parents eb4103c + 25af9ff commit bcb0c78

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
```java
2+
import java.awt.image.AreaAveragingScaleFilter;
3+
import java.util.*;
4+
import java.io.*;
5+
6+
class IOController {
7+
BufferedReader br;
8+
BufferedWriter bw;
9+
StringTokenizer st;
10+
11+
public IOController() {
12+
br = new BufferedReader(new InputStreamReader(System.in));
13+
bw = new BufferedWriter(new OutputStreamWriter(System.out));
14+
st = new StringTokenizer("");
15+
}
16+
17+
String nextLine() throws Exception {
18+
String line = br.readLine();
19+
st = new StringTokenizer(line);
20+
return line;
21+
}
22+
23+
String nextToken() throws Exception {
24+
while (!st.hasMoreTokens()) nextLine();
25+
return st.nextToken();
26+
}
27+
28+
int nextInt() throws Exception {
29+
return Integer.parseInt(nextToken());
30+
}
31+
32+
long nextLong() throws Exception {
33+
return Long.parseLong(nextToken());
34+
}
35+
36+
double nextDouble() throws Exception {
37+
return Double.parseDouble(nextToken());
38+
}
39+
40+
void close() throws Exception {
41+
bw.flush();
42+
bw.close();
43+
}
44+
45+
void write(String content) throws Exception {
46+
bw.write(content);
47+
}
48+
49+
}
50+
51+
public class Main {
52+
53+
static IOController io;
54+
55+
//
56+
57+
static final long MOD = 998244353;
58+
static long P, N, M;
59+
60+
public static void main(String[] args) throws Exception {
61+
62+
io = new IOController();
63+
64+
for(int T=io.nextInt(); T-->0; ) {
65+
init();
66+
solve();
67+
}
68+
69+
io.close();
70+
71+
}
72+
73+
static void init() throws Exception {
74+
75+
P = io.nextLong();
76+
N = io.nextLong();
77+
M = io.nextLong();
78+
79+
}
80+
81+
static void solve() throws Exception {
82+
83+
if(N < M) {
84+
long t = N;
85+
N = M;
86+
M = t;
87+
}
88+
89+
long pm = power(P, M);
90+
long ans = pm * ((power(pm, N/M) + MOD - 1) % MOD) % MOD;
91+
ans = (ans * (power((pm+MOD-1)%MOD, MOD-2))) % MOD;
92+
if(N%M != 0) ans = (ans + power(P, N)) % MOD;
93+
94+
if(pm == 1) {
95+
ans = N/M;
96+
if(N%M != 0) ans = (ans + power(P, N)) % MOD;
97+
}
98+
io.write(ans + "\n");
99+
100+
}
101+
102+
static long power(long a, long x) {
103+
if(x == 0) return 1;
104+
if(x == 1) return a%MOD;
105+
long h = power(a, x>>1) % MOD;
106+
if(x%2 == 0) return h*h%MOD;
107+
return h*h%MOD*a%MOD;
108+
}
109+
110+
}
111+
```

0 commit comments

Comments
 (0)