Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- long long fact[20];
- set<string> was;
- int c0[10];
- int c[10];
- void split(string s, int *c) {
- for (int i = 0; i < 10; i++)
- c[i] = 0;
- for (char ch : s)
- c[ch - 48]++;
- }
- long long getCount() {
- long long ans = fact[accumulate(c, c + 10, 0)];
- for (int i = 0; i < 10; i++)
- ans /= fact[c[i]];
- return ans;
- }
- long long getans(string s) {
- split(s, c);
- // check whether the string contains all digits
- for (int i = 0; i < 10; i++)
- if (c0[i] && !c[i])
- return 0;
- // check whether we already processed such string
- sort(s.begin(), s.end());
- if (was.count(s))
- return 0;
- was.insert(s);
- long long ans = getCount();
- if (c[0] > 0) {
- c[0]--;
- ans -= getCount();
- }
- return ans;
- }
- int main() {
- fact[0] = 1;
- for (int i = 1; i < 20; i++)
- fact[i] = i * fact[i - 1];
- string n;
- cin >> n;
- int k = n.length();
- split(n, c0);
- long long ans = 0;
- for (int i = 1; i <= (1 << k); i++) {
- string c;
- for (int j = 0; j < k; j++)
- if (i & (1 << j))
- c += n[j];
- ans += getans(c);
- }
- cout << ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement