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 61 62 63 64 65 66 67 68 69
| using namespace std;
typedef long long ll; ll dp[20][1 << 10][12]; int num[20]; ll l, r; int k;
int (int x, int y){ if(x & (1 << y)) return x; for(int i = y + 1; i < 10; i++){ if(!((1 << i) & x))continue; x -= (1 << i); x += (1 << y); return x; } x += (1 << y); return x; }
int count(int x){ int res = 0; for(int i = 0; i < 10; i++){ if(x & (1 << i))res++; } return res; }
ll dfs(int pos, int p, bool limit, bool lead){ if(pos < 0)return count(p) == k; if(!limit && !lead && dp[pos][p][k] != -1) return dp[pos][p][k]; ll ans = 0; int e = !limit ? 9 : num[pos]; if(lead){ ans += dfs(pos - 1, p, limit && num[pos] == 0, true); } else{ ans += dfs(pos - 1, get(p, 0), limit && num[pos] == 0, false); } for(int i = 1; i <= e; i++){ ans += dfs(pos - 1, get(p, i), limit && num[pos] == i, false); } if(!limit && !lead) dp[pos][p][k] = ans; return ans; }
ll solve(ll x){ int pos = 0; while(x){ num[pos++] = x % 10; x /= 10; } return dfs(pos - 1, 0, 1, 1); }
int T;
int main(){ ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> T; memset(dp, -1, sizeof(dp)); for(int i = 1; i <= T; i++){ cin >> l >> r >> k; cout << "Case #" << i << ": " << solve(r) - solve(l - 1) << 'n'; } return 0; }
|