fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define MAX_CHAR 256
  4.  
  5. long long int fact(long long int n)
  6. {
  7. return (n <= 1)? 1 :n * fact(n-1);
  8. }
  9. void populateAndIncreaseCount (long long int* count, char* str)
  10. {
  11. long long int i;
  12.  
  13. for( i = 0; str[i]; ++i )
  14. ++count[ str[i] ];
  15.  
  16. for( i = 1; i < 256; ++i )
  17. count[i] += count[i-1];
  18. }
  19.  
  20. void updatecount (long long int* count, char ch)
  21. {
  22. long long int i;
  23. for( i = ch; i < MAX_CHAR; ++i )
  24. --count[i];
  25. }
  26.  
  27.  
  28. long long int findRank (char* str)
  29. {
  30. long long int len = strlen(str);
  31. long long int mul = fact(len);
  32. long long int rank = 1, i;
  33. long long int count[MAX_CHAR] = {0}; // all elements of //count[] are initialized with 0
  34.  
  35. populateAndIncreaseCount( count, str );
  36.  
  37. for (i = 0; i < len; ++i)
  38. {
  39. mul /= len - i;
  40.  
  41. rank += count[ str[i] - 1] * mul;
  42.  
  43. updatecount (count, str[i]);
  44. }
  45.  
  46. return rank;
  47. }
  48.  
  49.  
  50. int main()
  51. {
  52. int n;
  53. long long int res;
  54. char string[10];
  55. scanf("%d", &n);
  56. res = 1;
  57. while(n--){
  58. scanf("%s",string);
  59. res *= findRank(string)%23456;
  60. }
  61. printf ("%lld\n", res%23456);
  62. return 0;
  63. }
Success #stdin #stdout 0s 10320KB
stdin
4 
0123456798 
0123456879 
0123456897 
0123456978
stdout
120