Advertisement
a53

nozero

a53
May 7th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cmath>
  3. #include <vector>
  4. #include <map>
  5. using namespace std;
  6. #define NMAX 100005
  7. map<int, bool> apparition;
  8. vector<int> digits;
  9. int n, k, answer;
  10. long long fact[NMAX];
  11.  
  12. int computeFactorials(int n, int k) {
  13. fact[0] = 1;
  14. for(int i = 1; i <= n; i++) {
  15. fact[i] = fact[i - 1] * i;
  16. if(fact[i] > k)
  17. return i - 1;
  18. }
  19. return n;
  20. }
  21.  
  22. int countFixedSolutions(int max_elem) {
  23. int answer = 0;
  24. if(!max_elem)
  25. return 0;
  26. while(max_elem) {
  27. digits.push_back(max_elem % 10);
  28. max_elem /= 10;
  29. }
  30. for(int i = 1; i < (int)digits.size(); i++) {
  31. answer += pow(9, i);
  32. }
  33.  
  34. for(int i = digits.size() - 1; i >= 0; i--) {
  35. if(!digits[i])
  36. return answer;
  37. answer += pow(9, i) * (digits[i] - 1);
  38. }
  39. return answer + 1;
  40. }
  41.  
  42. int noZeroVerdict(int value) {
  43. if(!value)
  44. return 1;
  45. return ((value % 10 != 0) & noZeroVerdict(value / 10));
  46. }
  47.  
  48. int nextActiveValue(int value) {
  49. value++;
  50. while(value < n && apparition[value]) {
  51. value++;
  52. }
  53. return value;
  54. }
  55.  
  56. int main ()
  57. {
  58.  
  59. freopen("nozero.in", "r", stdin);
  60. freopen("nozero.out", "w", stdout);
  61. scanf("%d%d", &n, &k);
  62. k--;
  63. int last = computeFactorials(n, k);
  64. int answer = countFixedSolutions(n - last - 1);
  65. for(int i = n - last; i <= n; i++) {
  66. int level = n - i;
  67. int value = nextActiveValue(n - last - 1);
  68. while(k >= fact[level]) {
  69. k -= fact[level];
  70. value = nextActiveValue(value);
  71. }
  72. apparition[value] = true;
  73. answer += (noZeroVerdict(value) & noZeroVerdict(i));
  74. }
  75. printf("%d\n", answer);
  76. return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement