페이지

레이블이 Big Integer인 게시물을 표시합니다. 모든 게시물 표시
레이블이 Big Integer인 게시물을 표시합니다. 모든 게시물 표시

1793번: 타일링

https://www.acmicpc.net/problem/1793


$O(NL+t)$ // N,L은 각각 최댓값


#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
string f(string a, string b) {
    int t = 0;
    string r;
    while (!a.empty() || !b.empty() || t) {
        if (!a.empty()) t += a.back() - '0', a.pop_back();
        if (!b.empty()) t += b.back() - '0', b.pop_back();
        r += t % 10 + '0';
        t /= 10;
    }
    reverse(r.begin(), r.end());
    return r;
}
string dp[251] = { "1","1" };
int n;
int main() {
    for (int i = 2; i <= 250; i++) dp[i] = f(f(dp[i - 2], dp[i - 2]), dp[i - 1]);
    while (cin >> n) cout << dp[n] << endl;
    return 0;
}

10757번: 큰 수 A+B

https://www.acmicpc.net/problem/10757


$O(l1+l2)$


#include<cstdio>
#include<string.h>
char s1[10001], s2[10001], r[10002];
int l1, l2, p = 10001, t;
int main() {
    scanf("%s %s", s1, s2);
    l1 = strlen(s1) - 1;
    l2 = strlen(s2) - 1;
    while (l1 > -1 || l2 > -1 || t) {
        if (l1 > -1) t += s1[l1--] - '0';
        if (l2 > -1) t += s2[l2--] - '0';
        r[--p] = t % 10 + '0';
        t /= 10;
    }
    printf("%s", r + p);
    return 0;
}