Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define ll long long
- #define endl '\n'
- #define IOS ios::sync_with_stdio(0); cin.tie(0);
- #define FIO freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
- #define MOD 1000000007LL
- //#define MOD 998244353LL
- #define all(x) (x).begin(),(x).end()
- using namespace std;
- vector<vector<vector<ll>>> dp;
- ll f(int rem, int i, string mx, bool isLess){ // This function counts total number of x, having sumOfDigits(x) == s in range 1 to mx
- if(rem < 0) return 0;
- if(rem == 0) return 1;
- if(i == mx.length()) return 0;
- if(dp[rem][i][isLess] != -1) return dp[rem][i][isLess];
- int d = mx[i] - '0';
- ll ret = 0;
- if(!isLess){ // We can choose digits from 0 to mx[i]
- for(int currD = 0; currD < d; currD++) ret += f(rem - currD, i + 1, mx, true);
- ret += f(rem - d, i + 1, mx, false);
- }
- else{ // We can choose any digit from 0 to 10
- for(int currD = 0; currD < 10; currD++) ret += f(rem - currD, i + 1, mx, true);
- }
- dp[rem][i][isLess] = ret;
- return ret;
- }
- void init(int s){
- dp.clear();
- dp.resize(s + 1, vector<vector<ll>> (20, vector<ll> (2, -1)));
- }
- int main(){
- #ifndef ONLINE_JUDGE
- FIO
- #endif
- IOS
- ll low, hi;
- cin >> low >> hi;
- ll sum = 0, maxCount = 0;
- for(int s = 1; s <= 162; s++){ // maximum sum can be 18 * 9 = 162.
- init(s);
- ll uptoHi = f(s, 0, to_string(hi), false);
- init(s);
- ll uptoLow = f(s, 0, to_string(low - 1), false);
- if(maxCount <= uptoHi - uptoLow){
- maxCount = uptoHi - uptoLow;
- sum = s;
- }
- }
- cout << "For sum = " << sum << " number of winner: " << maxCount << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment