Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define int long long
- #define ull unsigned long long
- #define ll long long
- #define MM 1000000007
- #define N 100005
- #define pb push_back
- #define p_q priority_queue
- #define pii pair<ll,ll>
- #define vi vector<ll>
- #define vii vector<pii>
- #define mi map<ll,ll>
- #define mii map<pii,ll>
- #define all(a) (a).begin(),(a).end()
- #define sz(x) (ll)x.size()
- #define endl '\n'
- #define Endl '\n'
- #define gcd(a,b) __gcd((a),(b))
- #define lcm(a,b) ((a)*(b)) / gcd((a),(b))
- #define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
- #define mp make_pair
- #define lb lower_bound
- #define ub upper_bound
- #define F first
- #define S second
- #define rep(i, begin, end) for(int i=begin;i<end;i++)
- #define repr(i,begin,end) for(int i=end-1;i>=begin;i--)
- #define ini(a,n,b) for(ll int i=0;i<n;i++) a[i]=0;
- #define cset(a) __builtin_popcountll(a)
- #define hell (ull)1e9
- #define re resize
- int fact[21];
- int expo(int base)
- {
- int pow=MM-2;
- int res=1;
- while(pow)
- {
- if(pow&1)
- {
- res=(res*base)%MM;
- }
- base=(base*base)%MM;
- pow/=2;
- }
- return res;
- }
- int count(string s)
- {
- int ans=fact[s.length()];
- vector<int> cnt(26,0);
- for(char i:s) cnt[i-'a']++;
- for(int i=0;i<26;i++)
- {
- if(cnt[i])
- {
- ans=(ans*expo(cnt[i]))%MM;
- }
- }
- return ans;
- }
- int fun(string s,int p)
- {
- int n=s.length();
- int ans=0;
- fact[0]=1;
- for(int i=1;i<=n;i++)
- {
- fact[i]=(fact[i-1]*i)%MM;
- }
- for(int i=0;i<(1<<n);i++)
- {
- string curr;
- int sum=0;
- for(int j=0;j<n;j++)
- {
- if(i & (1<<j))
- {
- curr.push_back(s[j]);
- sum+=s[j];
- }
- }
- if(sum and sum%p==0)
- {
- ans+=count(curr);
- }
- }
- return ans;
- }
- signed main(void)
- {ios
- int TESTS=1;
- //cin>>TESTS;
- while(TESTS--)
- {
- string s;
- cin>>s;
- int p;
- cin>>p;
- cout<<fun(s,p);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement