2017 ACM/ICPC 沈阳 I题 Little Boxes
Little boxes on the hillside.
Little boxes made of ticky-tacky.
Little boxes.
Little boxes.
Little boxes all the same.
There are a green boxes, and b pink boxes.
And c blue boxes and d yellow boxes.
And they are all made out of ticky-tacky.
And they all look just the same.
Little boxes made of ticky-tacky.
Little boxes.
Little boxes.
Little boxes all the same.
There are a green boxes, and b pink boxes.
And c blue boxes and d yellow boxes.
And they are all made out of ticky-tacky.
And they all look just the same.
InputThe input has several test cases. The first line contains the integer t (1 ≤ t ≤ 10) which is the total number of test cases.
For each test case, a line contains four non-negative integers a, b, c and d where a, b, c, d ≤ 2^62, indicating the numbers of green boxes, pink boxes, blue boxes and yellow boxes.
OutputFor each test case, output a line with the total number of boxes.
Sample Input
4 1 2 3 4 0 0 0 0 1 0 0 0 111 222 333 404
Sample Output
10 0 1 1070
题解: 题意就是求a+b+c+d的结果
C++大数模板求即可Orz
1 #include <bits/stdc++.h> 2 using namespace std; 3 // base and base_digits must be consistent 4 constexpr int base = 1000000000; 5 constexpr int base_digits = 9; 6 struct bigint{ 7 vector<int> z; 8 int sign; 9 bigint() : sign(1) {} 10 bigint(long long v) { *this = v; } 11 bigint& operator=(long long v) 12 { 13 sign = v < 0 ? -1 : 1; 14 v*=sign; 15 z.clear(); 16 for(; v > 0; v = v / base) z.push_back((int)(v % base)); 17 return *this; 18 } 19 20 bigint(const string& s) { read(s); } 21 22 bigint& operator+=(const bigint& other) 23 { 24 if (sign == other.sign) 25 { 26 for (int i = 0, carry = 0; i < other.z.size() || carry; ++i) 27 { 28 if(i==z.size()) z.push_back(0); 29 z[i] += carry + (i < other.z.size() ? other.z[i] : 0); 30 carry = z[i] >= base; 31 if(carry) z[i] -= base; 32 } 33 } 34 else if (other != 0 /* prevent infinite loop */) 35 { 36 *this -= -other; 37 } 38 return *this; 39 } 40 41 friend bigint operator+(bigint a, const bigint& b) 42 { 43 return a += b; 44 } 45 46 bigint& operator-=(const bigint& other) 47 { 48 if (sign == other.sign) 49 { 50 if (sign == 1 && *this >= other || sign == -1 && *this <= other) 51 { 52 for (int i = 0, carry = 0; i < other.z.size() || carry; ++i) 53 { 54 z[i] -= carry + (i < other.z.size() ? other.z[i] : 0); 55 carry = z[i] < 0; 56 if(carry) z[i] += base; 57 } 58 trim(); 59 } 60 else 61 { 62 *this = other - *this; 63 this->sign = -this->sign; 64 } 65 } 66 else *this += -other; 67 return *this; 68 } 69 70 friend bigint operator - (bigint a,const bigint& b) 71 { 72 return a -= b; 73 } 74 75 bigint& operator*=(int v) 76 { 77 if(v<0) sign=-sign,v=-v; 78 for(int i=0,carry=0;i<z.size() || carry;++i) 79 { 80 if(i==z.size()) z.push_back(0); 81 long long cur = (long long)z[i] * v + carry; 82 carry = (int)(cur / base); 83 z[i] = (int)(cur % base); 84 } 85 trim(); 86 return *this; 87 } 88 89 bigint operator*(int v) const 90 { 91 return bigint(*this) *= v; 92 } 93 94 friend pair<bigint, bigint> divmod(const bigint& a1, const bigint& b1) 95 { 96 int norm = base / (b1.z.back() + 1); 97 bigint a = a1.abs() * norm; 98 bigint b = b1.abs() * norm; 99 bigint q, r; 100 q.z.resize(a.z.size()); 101 102 for (int i = (int)a.z.size() - 1; i >= 0; i--) 103 { 104 r*=base; r+=a.z[i]; 105 int s1 = b.z.size() < r.z.size() ? r.z[b.z.size()] : 0; 106 int s2 = b.z.size() - 1 < r.z.size() ? r.z[b.z.size() - 1] : 0; 107 int d = (int)(((long long)s1 * base + s2) / b.z.back()); 108 r -= b * d; 109 while(r < 0) r+=b,--d; 110 q.z[i] = d; 111 } 112 113 q.sign = a1.sign * b1.sign; 114 r.sign = a1.sign; 115 q.trim(); 116 r.trim(); 117 return {q, r / norm}; 118 } 119 120 friend bigint sqrt(const bigint& a1) 121 { 122 bigint a=a1; 123 while(a.z.empty()||a.z.size()%2==1) a.z.push_back(0); 124 125 int n = a.z.size(); 126 int firstDigit = (int)::sqrt((double)a.z[n - 1] * base + a.z[n - 2]); 127 int norm = base / (firstDigit + 1); 128 a *= norm; 129 a *= norm; 130 while(a.z.empty()||a.z.size()%2==1) a.z.push_back(0); 131 132 bigint r = (long long)a.z[n - 1] * base + a.z[n - 2]; 133 firstDigit = (int)::sqrt((double)a.z[n - 1] * base + a.z[n - 2]); 134 int q = firstDigit; 135 bigint res; 136 for (int j = n / 2 - 1; j >= 0; j--) 137 { 138 for(;;--q) 139 { 140 bigint r1=(r-(res*2*base+q)*q)*base*base+(j>0?(long long)a.z[2*j-1]*base+a.z[2*j-2]:0); 141 if(r1>=0) { r=r1; break; } 142 } 143 res*=base;res+=q; 144 if(j>0) 145 { 146 int d1 = res.z.size() + 2 < r.z.size() ? r.z[res.z.size() + 2] : 0; 147 int d2 = res.z.size() + 1 < r.z.size() ? r.z[res.z.size() + 1] : 0; 148 int d3 = res.z.size() < r.z.size() ? r.z[res.z.size()]:0; 149 q = (int)(((long long)d1*base*base+(long long)d2*base+d3)/(firstDigit*2)); 150 } 151 } 152 153 res.trim(); 154 return res / norm; 155 } 156 157 bigint operator/(const bigint& v) const 158 { 159 return divmod(*this, v).first; 160 } 161 162 bigint operator%(const bigint& v) const 163 { 164 return divmod(*this, v).second; 165 } 166 167 bigint& operator/=(int v) 168 { 169 if(v<0) sign=-sign,v=-v; 170 for (int i = (int)z.size() - 1, rem = 0; i >= 0; --i) 171 { 172 long long cur = z[i] + rem * (long long)base; 173 z[i] = (int)(cur / v); 174 rem = (int)(cur % v); 175 } 176 trim(); 177 return *this; 178 } 179 180 bigint operator/(int v) const 181 { 182 return bigint(*this) /= v; 183 } 184 185 int operator%(int v) const 186 { 187 if(v<0) v=-v; 188 int m=0; 189 for(int i=(int)z.size()-1;i>=0;--i) m=(int)((z[i]+m*(long long)base)%v); 190 return m * sign; 191 } 192 193 bigint& operator*=(const bigint& v) 194 { 195 *this = *this * v; 196 return *this; 197 } 198 199 bigint& operator/=(const bigint& v) 200 { 201 *this = *this / v; 202 return *this; 203 } 204 205 bool operator<(const bigint& v) const 206 { 207 if(sign!=v.sign) return sign < v.sign; 208 if(z.size()!=v.z.size()) return z.size()*sign<v.z.size()*v.sign; 209 for(int i = (int)z.size() - 1; i >= 0; i--) 210 if(z[i] != v.z[i]) return z[i] * sign < v.z[i] * sign; 211 return false; 212 } 213 214 bool operator>(const bigint& v) const { return v < *this; } 215 bool operator<=(const bigint& v) const { return !(v < *this); } 216 bool operator>=(const bigint& v) const { return !(*this < v); } 217 bool operator==(const bigint& v) const { return !(*this < v) && !(v < *this); } 218 bool operator!=(const bigint& v) const { return *this < v || v < *this; } 219 220 void trim() 221 { 222 while(!z.empty() && z.back() == 0) z.pop_back(); 223 if(z.empty()) sign = 1; 224 } 225 226 bool isZero() const { return z.empty(); } 227 228 friend bigint operator-(bigint v) 229 { 230 if(!v.z.empty()) v.sign = -v.sign; 231 return v; 232 } 233 234 bigint abs() const 235 { 236 return sign == 1 ? *this : -*this; 237 } 238 239 long long longValue() const 240 { 241 long long res = 0; 242 for(int i = (int)z.size() - 1; i >= 0; i--) res = res * base + z[i]; 243 return res * sign; 244 } 245 246 friend bigint gcd(const bigint& a, const bigint& b) 247 { 248 return b.isZero() ? a : gcd(b, a % b); 249 } 250 251 friend bigint lcm(const bigint& a, const bigint& b) 252 { 253 return a / gcd(a, b) * b; 254 } 255 256 void read(const string& s) 257 { 258 sign = 1; 259 z.clear(); 260 int pos = 0; 261 while(pos < s.size() && (s[pos] == '-' || s[pos] == '+')) 262 { 263 if(s[pos] == '-') sign = -sign; 264 ++pos; 265 } 266 for(int i=(int)s.size()-1;i>=pos;i-=base_digits) 267 { 268 int x=0; 269 for(int j=max(pos,i-base_digits+1);j<=i;j++) x=x*10+s[j]-'0'; 270 z.push_back(x); 271 } 272 trim(); 273 } 274 275 friend istream& operator>>(istream& stream, bigint& v) 276 { 277 string s; 278 stream >> s; 279 v.read(s); 280 return stream; 281 } 282 283 friend ostream& operator<<(ostream& stream, const bigint& v) 284 { 285 if(v.sign == -1) stream << '-'; 286 stream << (v.z.empty() ? 0 : v.z.back()); 287 for(int i = (int)v.z.size() - 2; i >= 0; --i) 288 stream << setw(base_digits) << setfill('0') << v.z[i]; 289 return stream; 290 } 291 292 static vector<int> convert_base(const vector<int>& a, int old_digits, int new_digits) 293 { 294 vector<long long> p(max(old_digits, new_digits) + 1); 295 p[0] = 1; 296 for(int i=1;i<p.size();i++) p[i]=p[i-1]*10; 297 vector<int> res; 298 long long cur = 0; 299 int cur_digits = 0; 300 for(int v : a) 301 { 302 cur += v * p[cur_digits]; 303 cur_digits += old_digits; 304 while (cur_digits >= new_digits) 305 { 306 res.push_back(int(cur % p[new_digits])); 307 cur /= p[new_digits]; 308 cur_digits -= new_digits; 309 } 310 } 311 res.push_back((int)cur); 312 while(!res.empty() && res.back()==0) 313 res.pop_back(); 314 return res; 315 } 316 317 typedef vector<long long> vll; 318 static vll karatsubaMultiply(const vll& a, const vll& b) 319 { 320 int n=a.size(); 321 vll res(n + n); 322 if(n <= 32) 323 { 324 for (int i = 0; i < n; i++) 325 for (int j = 0; j < n; j++) 326 res[i + j] += a[i] * b[j]; 327 return res; 328 } 329 330 int k = n >> 1; 331 vll a1(a.begin(), a.begin() + k); 332 vll a2(a.begin() + k, a.end()); 333 vll b1(b.begin(), b.begin() + k); 334 vll b2(b.begin() + k, b.end()); 335 vll a1b1 = karatsubaMultiply(a1, b1); 336 vll a2b2 = karatsubaMultiply(a2, b2); 337 for(int i=0;i<k;i++) a2[i]+=a1[i]; 338 for(int i=0;i<k;i++) b2[i]+=b1[i]; 339 340 vll r = karatsubaMultiply(a2, b2); 341 for(int i=0;i<a1b1.size();i++) r[i]-=a1b1[i]; 342 for(int i=0;i<a2b2.size();i++) r[i]-=a2b2[i]; 343 for(int i=0;i<r.size();i++) res[i+k]+=r[i]; 344 for(int i=0;i<a1b1.size();i++) res[i]+=a1b1[i]; 345 for(int i = 0;i<a2b2.size();i++) res[i+n]+=a2b2[i]; 346 return res; 347 } 348 349 bigint operator*(const bigint& v) const 350 { 351 vector<int> a6=convert_base(this->z,base_digits,6); 352 vector<int> b6=convert_base(v.z,base_digits,6); 353 vll a(a6.begin(),a6.end()); 354 vll b(b6.begin(),b6.end()); 355 while(a.size()<b.size()) a.push_back(0); 356 while(b.size()<a.size()) b.push_back(0); 357 while(a.size()&(a.size()-1)) a.push_back(0),b.push_back(0); 358 vll c=karatsubaMultiply(a, b); 359 bigint res; 360 res.sign = sign * v.sign; 361 for (int i = 0, carry = 0; i < c.size(); i++) 362 { 363 long long cur = c[i] + carry; 364 res.z.push_back((int)(cur % 1000000)); 365 carry = (int)(cur / 1000000); 366 } 367 res.z = convert_base(res.z, 6, base_digits); 368 res.trim(); 369 return res; 370 } 371 }; 372 int main() 373 { 374 ios::sync_with_stdio(0); 375 cin.tie(0); 376 bigint a, b, c, d; 377 int T; cin >> T; 378 while(T--) 379 { 380 cin >>a>>b>>c>>d; 381 a=a+b+c+d; 382 cout<<a<<endl; 383 384 } 385 return 0; 386 }