Submission #82318


ソースコード

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include<bits/stdc++.h>
using namespace std;
template<int size>
struct BigInt {
bitset<size> bits;
//補数計算
BigInt<size> com() const {
BigInt<size> result = *this;
bool c = 1;
for(int i = 0;i < size;i++){
result.bits[i] = result.bits[i] ^ 1;
if(c & result.bits[i]){
result.bits[i] = 0;
}else if(c & !result.bits[i]){
result.bits[i] = 1;
c = 0;
}
}
return result;
}
// 加算
BigInt<size> add(const BigInt<size>& x) const {
BigInt<size> result = *this;
bool c = 0;
for (int i = 0; i < size; i++) {
bool sum = result.bits[i] ^ x.bits[i] ^ c;
c = (result.bits[i] & x.bits[i]) || (c & (result.bits[i] ^ x.bits[i]));
result.bits[i] = sum;
}
return result;
}
// 減算
BigInt<size> sub(const BigInt<size>& x) const {
BigInt<size> result = *this;
result = result.add(x.com());
return result;
}
// 乗算
BigInt<size> mul(const BigInt<size>& x) const {
BigInt<size> result;
BigInt<size> temp = *this;
for (int i = 0; i < size; i++) {
if (x.bits[i]) {
result = result.add(temp);
}
temp.bits <<= 1;
}
return result;
}
// 除算
BigInt<size> div(const BigInt<size>& x) const {
BigInt<size> ans;
BigInt<size> rem = *this;
BigInt<size> divisor = x;
int shift = 0;
bool mf = 0;
if(rem.bits[size-1]){
mf = mf ^ 1;
rem = rem.com();
}
if(divisor.bits[size-1]){
mf = mf ^ 1;
divisor = divisor.com();
}
if (x.bits.none()) {
std::cerr << "Error: Division by zero!" << std::endl;
return ans;
}
while (divisor <= rem) {
divisor.bits <<= 1;
shift++;
}
while (shift--) {
divisor.bits >>= 1;
ans.bits <<= 1;
if (rem >= divisor) {
rem = rem.sub(divisor);
ans.bits[0] = 1;
}
}
if(mf)ans = ans.com();
return ans;
}
// 余り
BigInt<size> mod(const BigInt<size>& x) const {
BigInt<size> rem = *this;
BigInt<size> divisor = x;
int shift = 0;
bool mf = 0;
if(rem.bits[size-1]){
mf = 1;
rem = rem.com();
}
if(divisor.bits[size-1]){
divisor = divisor.com();
}
if (x.bits.none()) {
std::cerr << "Error: Division by zero!" << std::endl;
return rem;
}
while (divisor <= rem) {
divisor.bits <<= 1;
shift++;
}
while (shift--) {
divisor.bits >>= 1;
if (rem >= divisor) {
rem = rem.sub(divisor);
}
}
if(mf)rem = rem.com();
return rem;
}
// 比較演算子 (<)
bool operator<(const BigInt<size>& x) const {
for (int i = size - 1; i >= 0; i--) {
if (bits[i] != x.bits[i]) {
return bits[i] < x.bits[i];
}
}
return false;
}
// 比較演算子 (<=)
bool operator<=(const BigInt<size>& x) const {
return !(*this > x);
}
// 比較演算子 (>)
bool operator>(const BigInt<size>& x) const {
for (int i = size - 1; i >= 0; i--) {
if (bits[i] != x.bits[i]) {
return bits[i] > x.bits[i];
}
}
return false;
}
// 比較演算子 (>=)
bool operator>=(const BigInt<size>& x) const {
return !(*this < x);
}
// コンストラクタ (文字列から変換)
BigInt(const string& s) {
bool mf = false;
int len = s.size();
vector<long long> chunks(size / 43 + 1);
if (s[0] == '-') {
mf = true;
}
for (int i = len, j = 0; i > mf; i -= 13, j++) {
int start = max((int)mf, i - 13);
string chunkStr = s.substr(start, i - start);
chunks[j] = stoll(chunkStr);
}
reverse(chunks.begin(), chunks.end());
for (auto chunk : chunks) {
bitset<size> chunkBits(chunk);
*this = this->mul(bitset<size>("10010001100001001110011100101010000000000000"));
*this = this->add(chunkBits);
}
if (mf)*this = this->com();
}
// コンストラクタ (bitsetから初期化)
BigInt() : bits(0) {}
BigInt(const bitset<size>& x) : bits(x) {}
string print(){
stack<int>st;
BigInt x = *this;
bitset<4>a;
bool mf = 0;
if(x.bits[size-1]){
mf = 1;
x = x.com();
}
if(x.bits.none()){
return "0";
}
while(!x.bits.none()){
BigInt y = x.mod(bitset<size>("1010"));
a[0] = y.bits[0];
a[1] = y.bits[1];
a[2] = y.bits[2];
a[3] = y.bits[3];
st.push(a.to_ullong());
x = x.div(bitset<size>("1010"));
}
string s(st.size()+mf,'@');
for(int i = mf;!st.empty();i++){
s[i] = st.top()+'0';
st.pop();
}
if(mf)s[0] = '-';
return s;
}
};
int main(){
cin.tie(nullptr);ios::sync_with_stdio(false);
cout << std::fixed << std::setprecision(10);
string s,t;
cin >>s>>t;
BigInt<333340> a(s),b(t);
a = a.add(b);
// cout <<a.bits<<"\n";
cout <<a.print()<<"\n";
}
/*
やること
四則演算の記号の使用
stream処理
*/

ステータス

項目 データ
問題 1944 - 足し算をしよう
ユーザー名 ei2332
投稿日時 2025-01-10 08:43:52
言語 C++17
状態 Time Limit Exceeded
得点 0
ソースコード長 5564 Byte
最大実行時間 1000 ms
最大メモリ使用量 1116 KB

セット

セット 得点 Cases
1 ALL 0 / 10000 *

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
in01.txt TLE 1000 ms 1116 KB
1
in02.txt TLE 1000 ms 720 KB
1
in03.txt TLE 1000 ms 844 KB
1
in04.txt TLE 1000 ms 824 KB
1
in05.txt TLE 1000 ms 808 KB
1
in06.txt TLE 1000 ms 1056 KB
1
in07.txt TLE 1000 ms 788 KB
1
in08.txt TLE 1000 ms 764 KB
1
in09.txt TLE 1000 ms 744 KB
1