0%
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| template<typename R, typename T> R sstream_cast(const T& o) { stringstream ss; ss << o; R result; ss >> result; return result; }
int P(int n,int k) { if(k>n)return 0; int res = 1; for(int i=n-k+1; i<=n; i++){ res *= i; } return res; }
int countDistinctDigitsNumbers(int n) { string s = sstream_cast<string>(n); int len = s.size();
int result = 0; for(int i=1;i<len;i++) { result += 9 * P(9, i-1); } result += (s[0]-'0'-1) * P(9, len-1); bool used[10] = {0}; used[s[0]-'0'] = true; for(int i=1;i<len;i++) { int cnt = 0; for(int k=0;k<s[i]-'0';k++) { if(!used[k]) { cnt++; } } result += cnt * P(9-i, len-1-i);
if(used[s[i]-'0']) break; else used[s[i]-'0'] = true; } return result; }
|