Cod sursa(job #2823824)

Utilizator UnknownPercentageBuca Mihnea-Vicentiu UnknownPercentage Data 29 decembrie 2021 20:37:11
Problema 12-Perm Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <bits/stdc++.h>

using namespace std;

inline void Open(const string Name) {
    #ifndef ONLINE_JUDGE
        (void)!freopen((Name + ".in").c_str(), "r", stdin);
        (void)!freopen((Name + ".out").c_str(), "w", stdout);
    #endif
}

int dp[5];
int N;

int add_(int a, int b) {
    a = (a + b) & 1048575;
    return a;
}

int poz(int idx) {
    if(idx <= 0) 
        return 0;

    if((idx & 3) == 0)
        return 4;

    return idx & 3;
}

void solve() {
    cin >> N;

    dp[1] = 1, dp[2] = 2, dp[3] = 6, dp[4] = 12;
    for(int i = 5;i <= N;i++)
        dp[poz(i)] = add_(add_(dp[poz(i - 1)], dp[poz(i - 3)]), (2 * (i - 2)) & 1048575);
    cout << dp[poz(N)];
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    Open("12perm");

    int T = 1;
    for(;T;T--) {
        solve();
    }

    return 0;
}