Skip to content

Commit 23ebad5

Browse files
authored
Merge pull request #687 from AlgorithmWithGod/khj20006
[20250818] BOJ / P5 / AND, OR, XOR 2 / 권혁준
2 parents 8f5b96c + 13dba7c commit 23ebad5

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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+
59+
static int N;
60+
static int[] a;
61+
62+
public static void main(String[] args) throws Exception {
63+
64+
io = new IOController();
65+
66+
init();
67+
solve();
68+
69+
io.close();
70+
71+
}
72+
73+
static void init() throws Exception {
74+
75+
N = io.nextInt();
76+
a = new int[N+1];
77+
for(int i=1;i<=N;i++) a[i] = io.nextInt();
78+
79+
}
80+
81+
static void solve() throws Exception {
82+
83+
io.write(and() + " " + or() + " " + xor());
84+
85+
}
86+
87+
static long and() {
88+
long res = 0;
89+
for(int k=0;k<30;k++) {
90+
for(int i=1;i<=N;) if((a[i] & (1<<k)) != 0) {
91+
int j = i;
92+
while(j<=N && (a[j] & (1<<k)) != 0) j++;
93+
long cnt = j-i;
94+
res = (res + ((cnt*(cnt+1)/2)%MOD) * (1<<k)%MOD) % MOD;
95+
i = j;
96+
}
97+
else i++;
98+
}
99+
return res;
100+
}
101+
102+
static long or() {
103+
long res = 0;
104+
for(int k=0;k<30;k++) {
105+
long coef = (long)N*(N+1)/2;
106+
for(int i=1;i<=N;) if((a[i] & (1<<k)) == 0) {
107+
int j = i;
108+
while(j<=N && (a[j] & (1<<k)) == 0) j++;
109+
long cnt = j-i;
110+
coef -= cnt*(cnt+1)/2;
111+
i = j;
112+
}
113+
else i++;
114+
res = (res + coef%MOD * (1<<k)%MOD) % MOD;
115+
}
116+
return res;
117+
}
118+
119+
static long xor() {
120+
long res = 0;
121+
for(int k=0;k<30;k++) {
122+
int[] c = new int[N+1];
123+
long odd = 0, even = 0;
124+
for(int i=1;i<=N;i++) {
125+
c[i] += c[i-1];
126+
if((a[i] & (1<<k)) != 0) c[i]++;
127+
if(c[i]%2 == 0) {
128+
if(c[i] > 0) even++;
129+
}
130+
else odd++;
131+
}
132+
for(int i=1;i<=N;i++) {
133+
if(c[i-1]%2 == 0) res += odd*(1<<k);
134+
else res += even*(1<<k);
135+
res %= MOD;
136+
if(c[i]%2 == 0) {
137+
if(c[i] > 0) even--;
138+
}
139+
else odd--;
140+
}
141+
}
142+
return res;
143+
}
144+
145+
}
146+
```

0 commit comments

Comments
 (0)