-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdp digito.cpp
More file actions
62 lines (38 loc) · 912 Bytes
/
dp digito.cpp
File metadata and controls
62 lines (38 loc) · 912 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <bits/stdc++.h>
using namespace std;
//dp de digito
int n;
int dp[200][2];
vector <int> num;
int solve(int pos,int menor){//conta a soma de todos os dígitos dos números no intervalo de 0 a n
if(dp[pos][menor]!=-1) return dp[pos][menor];//caso calculado
if(pos==num.size()) return dp[pos][menor] = 0;//já escolheu todos os dígitos
//solução geral
int lim;
if(menor){
lim = 9;
}else{
lim = num[pos];
}
int resp = 0;
for(int i=0;i<=lim;i++){
int aux = menor;
if(i < num[pos]) aux = 1;
resp += i + solve(pos+1,aux);
}
return dp[pos][menor] = resp;
}
int main(){
memset(dp,-1,sizeof(dp));
cin >> n;
vector <int> arr;
while(n!=0){
arr.push_back(n%10);
n/=10;
}
for(int i=arr.size()-1;i>=0;i--){
num.push_back(arr[i]);
}
cout << solve(0,0) << endl;
return 0;
}