ProgMe

Untitled

Apr 5th, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define int int64_t
  3.  
  4. using namespace std;
  5.  
  6. int binpow(int x, int y)
  7. {
  8. if (y == 0)
  9. return 1;
  10. if (y & 1)
  11. return binpow(x, y - 1) * x;
  12. else
  13. {
  14. int ans = binpow(x, y / 2);
  15. return ans * ans;
  16. }
  17. }
  18.  
  19. int32_t main()
  20. {
  21. int t;
  22. cin >> t;
  23. while(t--)
  24. {
  25. string n;
  26. int k, ans = 0;
  27. deque <int> a (6);
  28. a = {LLONG_MIN, 0, 4, 6, 7, 9};
  29. cin >> k;
  30. n = to_string(k);
  31. if (n.size() > 1)
  32. ans = 5;
  33. for (int i = 1; i < n.size() - 1; i++)
  34. ans += binpow(5, i) * 4;
  35. bool can_make = 1;
  36. int j = lower_bound(a.begin(), a.end(), n.front() - '0') - a.begin() - 1;
  37. if (j < 1)
  38. can_make = 0;
  39. if (can_make)
  40. ans += binpow(5, n.size() - 1) * (--j);
  41. if (lower_bound(a.begin(), a.end(), n.front() - '0') == upper_bound(a.begin(), a.end(), n.front() - '0'))
  42. can_make = 0;
  43. for (int i = 1; i < n.size() - 1; i++)
  44. {
  45. j = lower_bound(a.begin(), a.end(), n [i] - '0') - a.begin();
  46. if (j < 1)
  47. can_make = 0;
  48. if (can_make)
  49. ans += binpow(5, n.size() - i - 1) * (--j);
  50. if (lower_bound(a.begin(), a.end(), n [i] - '0') == upper_bound(a.begin(), a.end(), n [i] - '0'))
  51. can_make = 0;
  52. }
  53. if (can_make)
  54. {
  55. if (lower_bound(a.begin(), a.end(), n.back() - '0') == upper_bound(a.begin(), a.end(), n.back() - '0'))
  56. ans += lower_bound(a.begin(), a.end(), n.back() - '0') - a.begin() - 1;
  57. else
  58. ans += lower_bound(a.begin(), a.end(), n.back() - '0') - a.begin();
  59. }
  60. cout << ans << "\n";
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment