Advertisement
at3107

Untitled

Aug 8th, 2020
1,188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define int         long long
  4. #define ull         unsigned long long
  5. #define ll          long long
  6. #define MM          1000000007
  7. #define N           100005
  8. #define pb          push_back
  9. #define p_q         priority_queue
  10. #define pii         pair<ll,ll>
  11. #define vi          vector<ll>
  12. #define vii         vector<pii>
  13. #define mi          map<ll,ll>
  14. #define mii         map<pii,ll>
  15. #define all(a)      (a).begin(),(a).end()
  16. #define sz(x)       (ll)x.size()
  17. #define endl        '\n'
  18. #define Endl        '\n'
  19. #define gcd(a,b)    __gcd((a),(b))
  20. #define lcm(a,b)    ((a)*(b)) / gcd((a),(b))
  21. #define ios         ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  22. #define mp          make_pair
  23. #define lb          lower_bound
  24. #define ub          upper_bound
  25. #define F           first
  26. #define S           second
  27. #define rep(i, begin, end) for(int i=begin;i<end;i++)
  28. #define repr(i,begin,end) for(int i=end-1;i>=begin;i--)
  29. #define ini(a,n,b)  for(ll int i=0;i<n;i++) a[i]=0;
  30. #define cset(a)     __builtin_popcountll(a)
  31. #define hell        (ull)1e9
  32. #define re          resize
  33.  
  34.  int fact[21];
  35.  
  36.  int expo(int base)
  37.  {
  38.      int pow=MM-2;
  39.      int res=1;
  40.      while(pow)
  41.      {
  42.          if(pow&1)
  43.          {
  44.              res=(res*base)%MM;
  45.          }
  46.          base=(base*base)%MM;
  47.          pow/=2;
  48.      }
  49.      return res;
  50.  }
  51.  int count(string s)
  52.  {
  53.      int ans=fact[s.length()];
  54.      vector<int> cnt(26,0);
  55.      for(char i:s) cnt[i-'a']++;
  56.      for(int i=0;i<26;i++)
  57.      {
  58.          if(cnt[i])
  59.          {
  60.              ans=(ans*expo(cnt[i]))%MM;
  61.          }
  62.      }
  63.      return ans;
  64.  }
  65.  
  66.  int fun(string s,int p)
  67.  {
  68.      int n=s.length();
  69.      int ans=0;
  70.      fact[0]=1;
  71.      for(int i=1;i<=n;i++)
  72.      {
  73.          fact[i]=(fact[i-1]*i)%MM;
  74.      }
  75.      for(int i=0;i<(1<<n);i++)
  76.      {
  77.          string curr;
  78.          int sum=0;
  79.          for(int j=0;j<n;j++)
  80.          {
  81.               if(i & (1<<j))
  82.               {
  83.                   curr.push_back(s[j]);
  84.                   sum+=s[j];
  85.               }  
  86.          }
  87.          if(sum and sum%p==0)
  88.          {
  89.              ans+=count(curr);
  90.          }
  91.      }
  92.      return ans;
  93.  }
  94.  
  95. signed main(void)
  96. {ios
  97.     int TESTS=1;
  98.     //cin>>TESTS;
  99.     while(TESTS--)
  100.     {
  101.         string s;
  102.         cin>>s;
  103.         int p;
  104.         cin>>p;
  105.         cout<<fun(s,p);
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement