Salvens

D

Aug 2nd, 2023
959
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <vector>
  5. #include <array>
  6. #include <set>
  7. #include <map>
  8.  
  9. using namespace std;
  10.  
  11. #define int long long
  12.  
  13. const long long INF = 1e18 + 7;
  14. const int MAXN = 1e1 + 100;
  15. const int N = 3e5 + 10;
  16. const int MOD = 1e9 + 7;
  17.  
  18. int dp[1 << 18][105];
  19.  
  20. signed main() {
  21.     ios_base::sync_with_stdio(false);
  22.     cin.tie(nullptr);
  23.     cout.tie(nullptr);
  24.     int n, m;
  25.     cin >> n >> m;
  26.     vector<int> a;
  27.     map<int, int> cnt;
  28.     while (n) {
  29.         a.emplace_back(n % 10);
  30.         ++cnt[n % 10];
  31.         n /= 10;
  32.     }
  33.     n = a.size();
  34.     dp[0][0] = 1;
  35.     for (int mask = 0; mask < (1 << n); ++mask) {
  36.         for (int rem = 0; rem < m; ++rem) {
  37.             for (int i = 0; i < n; ++i) {
  38.                 if (mask & (1 << i) || (mask == 0 && a[i] == 0)) continue;
  39.                 int new_rem = (rem * 10 + a[i]) % m;
  40.                 dp[mask | (1 << i)][new_rem] += dp[mask][rem];
  41.             }
  42.         }
  43.     }
  44.     int ans = dp[(1 << n) - 1][0];
  45.     for (auto [x, y]: cnt) {
  46.         for (int i = 2; i <= y; ++i) {
  47.             ans /= i;
  48.         }
  49.     }
  50.     cout << ans << '\n';
  51. }
Advertisement
Add Comment
Please, Sign In to add comment