Skip to content

Commit d24f433

Browse files
authored
[20250409] BOJ / P4 / 아 또 XOR이야? / 권혁준
1 parent d32fc92 commit d24f433

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
```java
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
class Main {
7+
8+
// IO field
9+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
11+
static StringTokenizer st = new StringTokenizer("");
12+
13+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
14+
static String nextToken() throws Exception {
15+
while(!st.hasMoreTokens()) nextLine();
16+
return st.nextToken();
17+
}
18+
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
19+
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
20+
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
21+
static void bwEnd() throws Exception {bw.flush();bw.close();}
22+
23+
// Additional field
24+
25+
static long N, A, B;
26+
static int K;
27+
static long[][] C;
28+
29+
public static void main(String[] args) throws Exception {
30+
31+
ready();
32+
solve();
33+
34+
bwEnd();
35+
36+
}
37+
38+
static void ready() throws Exception{
39+
40+
N = nextLong();
41+
K = nextInt();
42+
A = nextLong();
43+
B = nextLong();
44+
C = new long[62][62];
45+
C[0][0] = 1;
46+
for(int i=1;i<=61;i++) {
47+
C[i][0] = 1;
48+
for(int j=1;j<i;j++) C[i][j] = C[i-1][j-1] + C[i-1][j];
49+
C[i][i] = 1;
50+
}
51+
52+
}
53+
54+
static void solve() throws Exception{
55+
56+
bw.write((f(N,K,B) - f(N,K,A-1)) + "\n");
57+
58+
}
59+
60+
static long f(long n, int k, long x) {
61+
62+
if(x <= 0) return 0L;
63+
long res = 0, d = 1L;
64+
int nCount = bits(n);
65+
int zero = 0, one = 0;
66+
while((d<<1)-1 <= x) {
67+
int cur = (n&d) != 0 ? 1 : 0;
68+
nCount -= cur;
69+
int temp = k - nCount - (cur^1);
70+
71+
if(temp >= 0) {
72+
for(int zeroSelect = 0;zeroSelect<=zero;zeroSelect++) {
73+
int oneSelect = temp - zeroSelect;
74+
if(oneSelect > one) continue;
75+
if(oneSelect < 0) break;
76+
res += C[zero][zeroSelect] * C[one][oneSelect];
77+
}
78+
}
79+
80+
if(cur == 0) zero++;
81+
else one++;
82+
d<<=1;
83+
}
84+
if(x >= d) res += f(n^d, k, x-d) + (bits(n^d) == k ? 1 : 0);
85+
return res;
86+
87+
}
88+
89+
static int bits(long x) {
90+
int res = 0;
91+
for(int i=0;i<=60;i++) res += (x & (1L<<i)) == 0 ? 0 : 1;
92+
return res;
93+
}
94+
95+
}
96+
97+
```

0 commit comments

Comments
 (0)