Advertisement
daskalot

Untitled

Apr 23rd, 2019
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define f first
  4. #define s second
  5. #define pb push_back
  6.  
  7. using namespace std;
  8.  
  9. typedef long long ll;
  10.  
  11. string N, L, R;
  12.  
  13. ll fact[20];
  14.  
  15. ll solve(int mask, bool bot, bool up)
  16. {
  17. if (mask == (1 << N.size()) - 1) return 1;
  18. if (bot && up) return fact[N.size() - __builtin_popcount(mask)];
  19.  
  20. int ret = 0;
  21. for (int i = 0; i < N.size(); i++) {
  22. if (mask & (1 << i)) continue;
  23.  
  24. if (!bot && !up) {
  25. if (L[0] <= N[i] && N[i] <= R[0]) {
  26. ret += solve(mask | (1 << i), L[0] < N[i], N[i] < R[0]);
  27. }
  28.  
  29. continue;
  30. }
  31.  
  32. if (bot) {
  33. if (N[i] <= R[__builtin_popcount(mask)]) ret += solve(mask | (1 << i), true, up | (N[i] < R[__builtin_popcount(mask)]));
  34. }
  35.  
  36. if (up) {
  37. if (__builtin_popcount(mask) >= L.size() || N[i] >= L[__builtin_popcount(mask)]) ret += solve(mask | (1 << i), bot | (__builtin_popcount(mask) >= L.size() || N[i] > L[__builtin_popcount(mask)]), true);
  38. }
  39. }
  40.  
  41. return ret;
  42. }
  43.  
  44. int cc[10];
  45.  
  46. int main()
  47. {
  48. int t; cin >> t;
  49. while (t--) {
  50. fact[0] = 1ll;
  51. for (int i = 1; i < 20; i++) fact[i] = 1ll * fact[i - 1] * i;
  52. cin >> L >> R >> N;
  53. for (int i = 0; i < 10; i++) cc[i] = 0;
  54. for (int i = 0; i < N.size(); i++) cc[N[i] - '0']++;
  55. ll dv = 1ll;
  56. for (int i = 0; i < 10; i++) dv *= fact[cc[i]];
  57. cout << solve(0, 0, 0) / dv << endl;
  58. }
  59. return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement