Skip to content

Commit aa8330b

Browse files
authored
Merge pull request #236 from AlgorithmWithGod/khj20006
[20250312] BOJ / P4 / 이항 계수 5 / 권혁준
2 parents 15bffac + 6027dab commit aa8330b

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 boolean[] sieve;
23+
static long N, K, M;
24+
static long[] cnt;
25+
26+
public static void main(String[] args) throws Exception {
27+
28+
ready();
29+
solve();
30+
31+
bwEnd();
32+
33+
}
34+
35+
static void ready() throws Exception{
36+
37+
nextLine();
38+
N = nextLong();
39+
K = nextLong();
40+
M = nextLong();
41+
42+
sieve = new boolean[(int)N+1];
43+
cnt = new long[(int)N+1];
44+
45+
}
46+
47+
static void solve() throws Exception{
48+
49+
for(int i=2;i*i<=N;i++) if(!sieve[i]) for(int j=i*i;j<=N;j+=i) sieve[j] = true;
50+
51+
for(int i=2;i<=N;i++) if(!sieve[i]) for(long k=i;k<=N;k*=i) cnt[i] += N/k;
52+
for(int i=2;i<=N;i++) if(!sieve[i]) for(long k=i;k<=K;k*=i) cnt[i] -= K/k;
53+
for(int i=2;i<=N;i++) if(!sieve[i]) for(long k=i;k<=N-K;k*=i) cnt[i] -= (N-K)/k;
54+
55+
long ans = 1;
56+
for(int i=2;i<=N;i++) ans = (ans * power(i, cnt[i])) % M;
57+
bw.write(ans + "\n");
58+
59+
}
60+
61+
static long power(long x, long y) {
62+
if(y == 0) return 1;
63+
if(y == 1) return x%M;
64+
long h = power(x,y>>1) % M;
65+
h = (h*h)%M;
66+
if(y%2==0) return h;
67+
return h*x%M;
68+
}
69+
70+
}
71+
72+
```

0 commit comments

Comments
 (0)