回答編集履歴
2
ビット演算一般形
test
CHANGED
@@ -59,3 +59,81 @@
|
|
59
59
|
}
|
60
60
|
|
61
61
|
```
|
62
|
+
|
63
|
+
Javaの方がなじみあるのでJavaで書いてみますね。
|
64
|
+
|
65
|
+
BigInteger使うと簡単なんだろうけど、C++に標準の多倍長整数型がないようなので、配列で頑張った
|
66
|
+
|
67
|
+
最後だけめんどくさかったのでBitSetに変換して出力
|
68
|
+
|
69
|
+
```java
|
70
|
+
|
71
|
+
import java.util.*;
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
public class Main {
|
76
|
+
|
77
|
+
public static void main(String[] args) {
|
78
|
+
|
79
|
+
try (Scanner s = new Scanner(System.in)) {
|
80
|
+
|
81
|
+
int n = s.nextInt();
|
82
|
+
|
83
|
+
int sum = 0;
|
84
|
+
|
85
|
+
int[] a = new int[n];
|
86
|
+
|
87
|
+
for (int i = 0; i < n; i++) {
|
88
|
+
|
89
|
+
a[i] = s.nextInt();
|
90
|
+
|
91
|
+
sum += a[i];
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
long[] bit = new long[(sum >> 6) + 1];
|
96
|
+
|
97
|
+
bit[0] = 1;
|
98
|
+
|
99
|
+
sum = 0;
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
for (int i = 0; i < n; i++) {
|
104
|
+
|
105
|
+
int shiftCell = a[i] >> 6;
|
106
|
+
|
107
|
+
int shiftBit = a[i] & 0x3f;
|
108
|
+
|
109
|
+
for (int j = (sum >> 6); j >= 0; j--) {
|
110
|
+
|
111
|
+
if (shiftBit != 0) {
|
112
|
+
|
113
|
+
long upper = bit[j] >>> (64 - shiftBit);
|
114
|
+
|
115
|
+
if (upper != 0) {
|
116
|
+
|
117
|
+
bit[j + shiftCell + 1] |= upper;
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
bit[j + shiftCell] |= bit[j] << shiftBit;
|
124
|
+
|
125
|
+
}
|
126
|
+
|
127
|
+
sum += a[i];
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
System.out.println(BitSet.valueOf(bit));
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
```
|
1
=が必要だった
test
CHANGED
@@ -40,7 +40,7 @@
|
|
40
40
|
|
41
41
|
|
42
42
|
|
43
|
-
for(int S = 0; S < max; S++) {
|
43
|
+
for(int S = 0; S <= max; S++) {
|
44
44
|
|
45
45
|
if(x & (1 << S)) {
|
46
46
|
|