Cod sursa(job #2713522)

Utilizator PatrickCplusplusPatrick Kristian Ondreovici PatrickCplusplus Data 28 februarie 2021 10:41:00
Problema Nunta Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.07 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("nunta.in");
ofstream fout("nunta.out");

const int nmax = 1005;
int n, dp[3][nmax][50];

void aduna(int *a, int *b){
    int t = 0;
    for (int i = 1; i <= max(a[0], b[0]); ++i){
        if (i > a[0]) a[i] = 0;
        if (i > b[0]) b[i] = 0;
        a[i] = a[i] + b[i] + t;
        t = a[i] / 10000000;
        a[i] %= 10000000;
    }
    a[0] = max(a[0], b[0]);
    while (t){
        a[++a[0]] = t % 10000000;
        t = t / 10000000;
    }
}

int main(){
    fin >> n;
    dp[0][0][0] = dp[0][0][1] = 1;
    for (int i = 0; i <= n; ++i){
        for (int j = 0; j <= n; ++j){
            if (i == 0 && j == 0){
                continue;
            }
            if (i == j){
                dp[i % 3][j][0] = 1;
                dp[i % 3][j][1] = 0;
                if (i - 1 >= 0 && j - 1 >= 0){
                    aduna(dp[i % 3][j], dp[(i - 1) % 3][j - 1]);
                }
                if (j - 2 >= 0){
                    aduna(dp[i % 3][j], dp[i % 3][j - 2]);
                }
            }
            else{
                dp[i % 3][j][0] = 1;
                dp[i % 3][j][1] = 0;
                if (i - 2 >= 0){
                    aduna(dp[i % 3][j], dp[(i - 2) % 3][j]);
                }
            }
        }
    }
    for (int i = dp[n % 3][n][0]; i >= 1; --i){
        if (i != dp[n % 3][n][0]){
            if (dp[n % 3][n][i] <= 9){
                fout << "000000";
            }
            else if (dp[n % 3][n][i] <= 99){
                fout << "00000";
            }
            else if (dp[n % 3][n][i] <= 999){
                fout << "0000";
            }
            else if (dp[n % 3][n][i] <= 9999){
                fout << "000";
            }
            else if (dp[n % 3][n][i] <= 99999){
                fout << "00";
            }
            else if (dp[n % 3][n][i] <= 999999){
                fout << "0";
            }
        }
        fout << dp[n % 3][n][i];
    }
    fin.close();
    fout.close();
    return 0;
}