Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define int64_t long long
- using namespace std;
- int a, b;
- int64_t dp[20][180][2];
- vector<int> digit;
- void getDigits(int x) {
- digit.clear();
- while(x) {
- digit.push_back(x % 10);
- x /= 10;
- }
- }
- int64_t digitSum(int id, int sum, bool tight) {
- if(id == -1) return sum;
- if(dp[id][sum][tight] != -1 && !tight)
- return dp[id][sum][tight];
- int64_t res = 0;
- int k = (tight) ? digit[id] : 9;
- for(int i = 0; i <= k; ++i) {
- int newTight = tight & (digit[id] == i);
- res += digitSum(id - 1, sum + i, newTight);
- }
- if(!tight)
- dp[id][sum][tight] = res;
- return res;
- }
- int main() {
- #ifdef DN
- freopen("in.txt", "r", stdin);
- #endif
- //freopen("CPCRC1C.inp", "r", stdin);
- //freopen("CPCRC1C.out", "w", stdout);
- ios_base::sync_with_stdio(false);
- cin.tie(NULL); cout.tie(NULL);
- while(cin >> a >> b) {
- if(a == -1 && b == -1) break;
- fill(**dp, **dp + 20 * 180 * 2, -1);
- getDigits(a - 1);
- int64_t ans1 = digitSum(digit.size()-1, 0, true);
- fill(**dp, **dp + 20 * 180 * 2, -1);
- getDigits(b);
- int64_t ans2 = digitSum(digit.size()-1, 0, true);
- cout << ans2 - ans1 << '\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment