1793번 - 타일링
D[N] 은 2*N 직사각형을 채우는 방법의 수라고 할때
D[N]=D[N-1] + 2*D[N-2] 가 된다.
근데 이 문제의 가장 큰 문제점은 바로 답이 long long의 범위를 벗어난다는 점이다.
따라서 BigInteger 를 구현해야 하는데, 나는 이걸 구현해본적도 없고... 구현 할 생각도 없어서 인터넷에서 긁어왔다.
DP부분은 밑으 go() 함수만을 보면 된다.
<정답 코드>
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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 | #include<vector> #include <iostream> #include<string> #include<string.h> #include<algorithm> using namespace std; //typedef int64_t ll; typedef long long ll; typedef pair<ll, ll> lll; typedef pair<ll, int> lli; typedef pair<int, int> ii; #define EL printf("\n") #define OK printf("OK") #define pb push_back #define mp make_pair #define ep emplace_back #define X first #define Y second #define fillchar(a,x) memset(a, x, sizeof(a)) #define FOR(i,l,r) for (int i=l;i<=r;i++) #define FORD(i,r,l) for (int i=r;i>=l;i--) const int base = 1e9; typedef vector<int> BigInt; void Set(BigInt &a) { while (a.size() > 1 && a.back() == 0) a.pop_back(); } void Print(BigInt a) { Set(a); printf("%d", (a.size() == 0) ? 0 : a.back()); FORD(i, a.size() - 2, 0) printf("%09d", a[i]); EL; } BigInt Integer(string s) { BigInt ans; if (s[0] == '-') return ans; if (s.size() == 0) { ans.pb(0); return ans; } while (s.size() % 9 != 0) s = '0' + s; for (int i = 0; i<s.size(); i += 9) { int v = 0; for (int j = i; j<i + 9; j++) v = v * 10 + (s[j] - '0'); ans.insert(ans.begin(), v); } Set(ans); return ans; } BigInt Integer(char c[]) { string s = ""; FOR(i, 0, strlen(c) - 1) s = s + c[i]; return Integer(s); } BigInt Integer(ll x) { string s = ""; while (x > 0) s = char(x % 10 + '0') + s, x /= 10; return Integer(s); } BigInt Integer(int x) { return Integer((ll)x); } void operator >> (istream &in, BigInt &a) { string s; getline(cin, s); a = Integer(s); } void operator << (ostream &out, BigInt a) { Print(a); } bool operator < (BigInt a, BigInt b) { Set(a); Set(b); if (a.size() != b.size()) return (a.size() < b.size()); FORD(i, a.size() - 1, 0) if (a[i] != b[i]) return (a[i] < b[i]); return false; } bool operator > (BigInt a, BigInt b) { return (b < a); } bool operator == (BigInt a, BigInt b) { return (!(a < b) && !(b < a)); } bool operator <= (BigInt a, BigInt b) { return (a < b || a == b); } bool operator >= (BigInt a, BigInt b) { return (b < a || b == a); } bool operator < (BigInt a, int b) { return (a < Integer(b)); } bool operator > (BigInt a, int b) { return (a > Integer(b)); } bool operator == (BigInt a, int b) { return (a == Integer(b)); } bool operator >= (BigInt a, int b) { return (a >= Integer(b)); } bool operator <= (BigInt a, int b) { return (a <= Integer(b)); } BigInt max(BigInt a, BigInt b) { if (a > b) return a; return b; } BigInt min(BigInt a, BigInt b) { if (a < b) return a; return b; } BigInt operator + (BigInt a, BigInt b) { Set(a); Set(b); BigInt ans; int carry = 0; FOR(i, 0, max(a.size(), b.size()) - 1) { if (i < a.size()) carry += a[i]; if (i < b.size()) carry += b[i]; ans.pb(carry%base); carry /= base; } if (carry) ans.pb(carry); Set(ans); return ans; } BigInt operator + (BigInt a, int b) { return a + Integer(b); } BigInt operator ++ (BigInt &a) { // ++a a = a + 1; return a; } void operator += (BigInt &a, BigInt b) { a = a + b; } void operator += (BigInt &a, int b) { a = a + b; } BigInt operator - (BigInt a, BigInt b) { Set(a); Set(b); BigInt ans; int carry = 0; FOR(i, 0, a.size() - 1) { carry += a[i] - (i < b.size() ? b[i] : 0); if (carry < 0) ans.pb(carry + base), carry = -1; else ans.pb(carry), carry = 0; } Set(ans); return ans; } BigInt operator - (BigInt a, int b) { return a - Integer(b); } void operator -- (BigInt &a) { // --a a = a - 1; } void operator -= (BigInt &a, BigInt b) { a = a + b; } void operator -= (BigInt &a, int b) { a = a - b; } BigInt operator * (BigInt a, BigInt b) { Set(a); Set(b); BigInt ans; ans.assign(a.size() + b.size(), 0); FOR(i, 0, a.size() - 1) { ll carry = 0ll; for (int j = 0; j<b.size() || carry > 0; j++) { ll s = ans[i + j] + carry + (ll)a[i] * (j<b.size() ? (ll)b[j] : 0ll); ans[i + j] = s%base; carry = s / base; } } Set(ans); return ans; } BigInt operator * (BigInt a, int b) { return a * Integer(b); } void operator *= (BigInt &a, BigInt b) { a = a * b; } void operator *= (BigInt &a, int b) { a = a * b; } BigInt operator / (BigInt a, BigInt b) { Set(a); Set(b); if (b == Integer(0)) return Integer("-1"); BigInt ans, cur; FORD(i, a.size() - 1, 0) { cur.insert(cur.begin(), a[i]); int x = 0, L = 0, R = base; while (L <= R) { int mid = (L + R) >> 1; if (b*Integer(mid) > cur) { x = mid; R = mid - 1; } else L = mid + 1; } cur = cur - Integer(x - 1)*b; ans.insert(ans.begin(), x - 1); } Set(ans); return ans; } BigInt operator / (BigInt a, int b) { Set(a); BigInt ans; ll cur = 0ll; FORD(i, a.size() - 1, 0) { cur = (cur*(ll)base + (ll)a[i]); ans.insert(ans.begin(), cur / b); cur %= b; } Set(ans); return ans; } void operator /= (BigInt &a, BigInt b) { a = a / b; } void operator /= (BigInt &a, int b) { a = a / b; } BigInt operator % (BigInt a, BigInt b) { Set(a); Set(b); if (b == Integer(0)) return Integer("-1"); BigInt ans; FORD(i, a.size() - 1, 0) { ans.insert(ans.begin(), a[i]); int x = 0, L = 0, R = base; while (L <= R) { int mid = (L + R) >> 1; if (b*Integer(mid) > ans) { x = mid; R = mid - 1; } else L = mid + 1; } ans = ans - Integer(x - 1)*b; } Set(ans); return ans; } int operator % (BigInt a, int b) { Set(a); if (b == 0) return -1; int ans = 0; FORD(i, a.size() - 1, 0) ans = (ans*(base%b) + a[i] % b) % b; return ans; } void operator %= (BigInt &a, BigInt b) { a = a % b; } void operator %= (BigInt &a, int b) { a = a % Integer(b); } BigInt gcd(BigInt a, BigInt b) { Set(a); Set(b); while (b > Integer(0)) { BigInt r = a%b; a = b; b = r; } Set(a); return a; } BigInt lcm(BigInt a, BigInt b) { return (a*b / gcd(a, b)); } BigInt sqrt(BigInt a) { BigInt x0 = a, x1 = (a + 1) / 2; while (x1 < x0) { x0 = x1; x1 = (x1 + a / x1) / 2; } return x0; } BigInt pow(BigInt a, BigInt b) { if (b == Integer(0)) return Integer(1); BigInt tmp = pow(a, b / 2); if (b % 2 == 0) return tmp * tmp; return tmp * tmp * a; } BigInt pow(BigInt a, int b) { return pow(a, (Integer(b))); } int log(int n, BigInt a) { // log_n(a) Set(a); int ans = 0; while (a > Integer(1)) { ans++; a /= n; } return ans; } BigInt D[251]; int n; BigInt go(int k) { if (k < 0) { BigInt ret = Integer("0"); return ret; } if (k == 0) { BigInt ret = Integer("1"); return ret; } if (D[k] > 0) { return D[k]; } BigInt ret = Integer("0"); D[k] = ret; BigInt A = Integer("2"); D[k] = go(k - 1) + A * go(k - 2); return D[k]; } int main() { //freopen("input.txt", "r", stdin); //ios::sync_with_stdio(false); //cin.tie(NULL); BigInt ret = Integer("0"); for (int i = 0; i < 251; i++) { D[i] = ret; } while (scanf("%d",&n)!=EOF) { Print(go(n)); } return 0; } | cs |
반응형