-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ adds digit dp to find numbers which sum is divisible by d
- Loading branch information
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
algorithms/dynamic-programming/digits/sum-digits-divisible-by-d.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
7 changes: 7 additions & 0 deletions
7
algorithms/dynamic-programming/digits/sum-digits-divisible-by-d.tex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)$. | ||
|
||
|