spider68

IMP:Rank The Permutations

Jan 19th, 2021
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. long long int f[16];
  4. void fact(){
  5.     f[0]=1;
  6.     for(int i=1;i<=15;i++){
  7.         f[i]=i*f[i-1];
  8.         f[i]%=1000003;
  9.     }    
  10. }
  11. int main() {
  12.     long long int t,cnt;
  13.     cin>>t;
  14.     fact();
  15.     while(t--){
  16.         string s;
  17.         cin>>s;
  18.         set<char>set(s.begin(),s.end());
  19.         int l=s.length(),a[l];
  20.         if(set.size()!=l){
  21.             cout<<0<<endl;
  22.             continue;
  23.         }
  24.         for(int i=0;i<l;i++){
  25.             cnt=0;
  26.             for(int j=i+1;j<l;j++){
  27.                 if(s[i]>s[j])cnt++;
  28.             }
  29.             a[i]=cnt;
  30.         }
  31.         cnt=1;
  32.         for(int i=0;i<l;i++){
  33.             cnt+=a[i]*f[l-i-1];
  34.             cnt%=1000003;
  35.         }
  36.         cout<<cnt<<endl;
  37.     }
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment