Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define int int64_t
- using namespace std;
- int binpow(int x, int y)
- {
- if (y == 0)
- return 1;
- if (y & 1)
- return binpow(x, y - 1) * x;
- else
- {
- int ans = binpow(x, y / 2);
- return ans * ans;
- }
- }
- int32_t main()
- {
- int t;
- cin >> t;
- while(t--)
- {
- string n;
- int k, ans = 0;
- deque <int> a (6);
- a = {LLONG_MIN, 0, 4, 6, 7, 9};
- cin >> k;
- n = to_string(k);
- if (n.size() > 1)
- ans = 5;
- for (int i = 1; i < n.size() - 1; i++)
- ans += binpow(5, i) * 4;
- bool can_make = 1;
- int j = lower_bound(a.begin(), a.end(), n.front() - '0') - a.begin() - 1;
- if (j < 1)
- can_make = 0;
- if (can_make)
- ans += binpow(5, n.size() - 1) * (--j);
- if (lower_bound(a.begin(), a.end(), n.front() - '0') == upper_bound(a.begin(), a.end(), n.front() - '0'))
- can_make = 0;
- for (int i = 1; i < n.size() - 1; i++)
- {
- j = lower_bound(a.begin(), a.end(), n [i] - '0') - a.begin();
- if (j < 1)
- can_make = 0;
- if (can_make)
- ans += binpow(5, n.size() - i - 1) * (--j);
- if (lower_bound(a.begin(), a.end(), n [i] - '0') == upper_bound(a.begin(), a.end(), n [i] - '0'))
- can_make = 0;
- }
- if (can_make)
- {
- if (lower_bound(a.begin(), a.end(), n.back() - '0') == upper_bound(a.begin(), a.end(), n.back() - '0'))
- ans += lower_bound(a.begin(), a.end(), n.back() - '0') - a.begin() - 1;
- else
- ans += lower_bound(a.begin(), a.end(), n.back() - '0') - a.begin();
- }
- cout << ans << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment