forked from Ashishgup1/Competitive-Coding
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0792b63
commit 436b7bd
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
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,40 @@ | ||
//For Decimals | ||
|
||
int sz = 0; | ||
int x[20]; | ||
int cache[20][2][5]; | ||
|
||
int dp(int idx, bool less, int taken) | ||
{ | ||
if(taken > 3) | ||
return 0; | ||
if(idx == sz) | ||
return 1; | ||
int &ans = cache[idx][less][taken]; | ||
if(ans != -1) | ||
return ans; | ||
ans = 0; | ||
int lo = 0, hi = 9; | ||
if(!less) | ||
hi = x[idx]; | ||
for(int i = lo; i <= hi; i++) | ||
ans += dp(idx + 1, less | (i < x[idx]), taken + (i > 0)); | ||
return ans; | ||
} | ||
|
||
int f(int k) | ||
{ | ||
memset(cache, -1, sizeof(cache)); | ||
sz = 0; | ||
while(k > 0) | ||
{ | ||
x[sz++] = k % 10; | ||
k /= 10; | ||
} | ||
reverse(x, x + sz); | ||
int ans = dp(0, 0, 0); | ||
return ans; | ||
} | ||
|
||
//Problem 1: https://codeforces.com/contest/1036/problem/C | ||
|