1 #include <iostream> 2 #include <cstdlib> 3 #include <cstring> 4 #include <queue> 5 #include <cstdio> 6 #include <algorithm> 7 #include <map> 8 #define LL long long 9 10 using namespace std; 11 12 //X = a[1]*(n-1)!+a[2]*(n-2)!+...+a[i]*(n-i)!+...+a[n-1]*1!+a[n]*0! 13 //其中a[i]表示在num[i+1..n]中比num[i]小的数的数量 14 15 void Cantor(int nums[],int n) 16 { 17 int fact[15]; 18 fact[0] = 1; 19 fact[1] = 1; 20 for(int i = 2; i <= 13; i++) 21 fact[i] = fact[i-1]*i; 22 23 int cnt = 0; 24 for(int i = 1; i <= n; i++) 25 { 26 int tmp = 0; 27 for(int j = i+1; j <= n; j++) 28 if(nums[j] < nums[i]) 29 tmp++; 30 cnt += tmp * fact[n-i]; 31 } 32 printf("%d\n",cnt+1); 33 } 34 35 36 void solve() 37 { 38 int nums[1000]; 39 int n; 40 scanf("%d",&n); 41 for(int i = 1; i <= n; i++) 42 scanf("%d",&nums[i]); 43 Cantor(nums,n); 44 } 45 46 int main(void) 47 { 48 int t; 49 scanf("%d",&t); 50 while(t--) 51 { 52 solve(); 53 } 54 return 0; 55 }
时间: 2024-10-09 22:16:37