Submission

Status:

PPPPPPPPPPPPPPPPPPPP

Subtask/Task Score:

100/100

Score: 100

User: Bestzu

Problemset: สุ่มสลับ

Language: cpp

Time: 0.003 second

Submitted On: 2025-10-14 22:33:14

#include <bits/stdc++.h>
#define endl '\n'
using namespace std;

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

    int n; cin >> n;
    string s; cin >> s;

    vector<int> fact(n+1, 1);
    for(int i = 2; i <= n; i++) fact[i] = fact[i-1] * i;

    int order = 1;
    for(int i = 0; i < n; i++) {
    	int smaller = 0;
    	for(int j = i+1; j < n; j++) {
    		if(s[j] < s[i]) smaller++;
		}
		order += smaller * fact[n-(i+1)];
	}

    cout << order << endl;
    return 0;
}