Advertisement
Abrar_Al_Samit

Anagram Division (LOJ 1158)

Aug 3rd, 2021
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. string s;
  5. int k, N;
  6. int dp[1<<10][1001];
  7. int solve(int mask, int Mod) {
  8.     int &ret = dp[mask][Mod];
  9.     if(ret!=-1) return ret;
  10.     ret = 0;
  11.     if(mask==(1<<N)-1) {
  12.         return ret = (Mod == 0);
  13.     }
  14.     for(int i=0; i<N; ++i) if(~mask >> i & 1) {
  15.         int next_Mod = (10 * Mod + (s[i]-'0')) % k;
  16.         ret += solve(mask | (1<<i), next_Mod);
  17.     }
  18.     return ret;
  19. }
  20. int fact(int n) {
  21.     if(n < 2) return 1;
  22.     return n * fact(n-1);
  23. }
  24. void PlayGround() {
  25.     cin >> s >> k;
  26.     N = s.size();
  27.     for(int i=0; i<(1<<N); ++i) {
  28.         for(int j=0; j<k; ++j) {
  29.             dp[i][j] = -1;
  30.         }
  31.     }
  32.     int ans = solve(0, 0);
  33.     vector<int>freq(10);
  34.     for(char c : s) freq[c-'0']++;
  35.     for(auto it : freq) ans /= fact(it);
  36.     cout << ans << '\n';
  37.  
  38.     // cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
  39. }
  40. int main() {
  41.     int T; cin >> T;
  42.     for(int i=1; i<=T; ++i) {
  43.         cout << "Case " << i << ": ";
  44.         PlayGround();
  45.     }
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement