Skip to content

Commit

Permalink
✨ adds digit dp to find numbers which sum is divisible by d
Browse files Browse the repository at this point in the history
  • Loading branch information
iagorrr committed Jan 14, 2024
1 parent 6a944e9 commit bbd9bd4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const int MAXD(100), MAXKLEN(10000);
string K;
ll D;
vi digits;
ll _dp[MAXKLEN + 1][MAXD][2];
const ll MOD(1'000'000'007);
ll dp(int pos = 0, int md = 0, bool lim = 1) {
if (pos == len(digits)) return md == 0;

ll &ans = _dp[pos][md][lim];

if (ans != -1) {
return ans;
}

ans = 0;
int maxi = lim ? digits[pos] : 9;
for (int i = 0; i <= maxi; i++) {
int mdn = (md + i) % D;
ans = (ans + dp(pos + 1, mdn, lim & (i == maxi))) % MOD;
}

return ans;
}

ll solve(string x) {
digits.clear();
for (auto c : x) digits.emplace_back(c - '0');
memset(_dp, -1, sizeof _dp);
return dp();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
\subsubsection{Sum of digits divisible by $D$}

Find the total of numbers $X$, $1 \leq X \leq K$ such that the sum of digits in $X$ is divisible by $D$, as $K$ can be very large read it as string !

Solve $O(MAXKLEN \cdot MAXD)$.


0 comments on commit bbd9bd4

Please sign in to comment.