Submission #1268190

#TimeUsernameProblemLanguageResultExecution timeMemory
1268190discontinuousCryptography (NOI20_crypto)C++20
14 / 100
1096 ms35260 KiB
// Author: Anikait Prasar

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define pb push_back

const ll MOD = 1e9 + 7;
const ll INF = 1e15;
const ll N = 1e6;

ll n, m, k, a, b, c, d, h, l, r, x, y;

vector<int> arr(N);
vector<int> brr(N);

ll modPow(ll x, ll n) {
    ll res = 1;
    while(n >= 1) {
        if(n & 1) {
            res = (res * x) % MOD;            
            n--;  
        }        
        else {
            x = (x * x) % MOD;
            n /= 2;
        }
    }
    return res;
}

ll modInv(ll num) { return modPow(num, MOD - 2); }


vector<int> adj[N+1];
vector<int> visited(N);

void dfs(int node) {
    visited[node] = true;
    for(auto j : adj[node]) {
        if(!visited[j]) dfs(j);
    }
}

vector<ll> fact(N);

void solve() {
    cin >> n;
    vector<int> all;
    for(int j = 0; j<n; j++) {
        cin >> arr[j];
        all.pb(arr[j]);
    }

    sort(all.begin(), all.end());
    map<int, int> pos;

    for(int j = 0; j<n; j++) {
        pos[all[j]] = j; 
    }

    c = 0;
    for(int j = 0; j<n; j++) {
        // no of elements smaller
        m = pos[arr[j]];
        for(int i = 0; i<j; i++) {
            if(arr[i] < arr[j]) m--;
        }
        c += m*fact[n-j-1];
    }

    cout << c+1;
}

/**
----------------------------------------------------
Problem Notes :-


----------------------------------------------------
**/

int main() {
    ios::sync_with_stdio(false);
    cout.tie(0); cin.tie(0);

    fact[0] = 1;
    for(int j = 1; j<=N; j++) {
        fact[j] = (fact[j-1]*j)%MOD; 
    }

    int tc = 1; 
    // cin >> tc;

    while(tc--) {
        solve();
        cout << "\n";
    }

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...