-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoving Digits.cpp
More file actions
152 lines (125 loc) · 2.49 KB
/
Removing Digits.cpp
File metadata and controls
152 lines (125 loc) · 2.49 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lld double
int mod=1e9+7;
const int array_size = 1e6+100;
bool primes[array_size];
ll gcd(ll a, ll b){if (b == 0)return a;return gcd(b, a % b);}
ll lcm(ll a, ll b){return ((a/gcd(a,b))*b);}
void sieve(){
primes[0] = false;
primes[1] = false;
for(ll i = 2;i*i<=array_size;i++){
if(primes[i]){
for(ll j=i*i;j<=array_size;j+=i)
primes[j] = false;
}
}
}
vector<int> toVec(int n){
vector<int> a;
while(n>0){
int r = n%10;
a.push_back(r);
n/=10;
}
return a;
}
int find(int n,vector<int>& dp){
if(n==0)return 0;
if(n<=9)return 1;
if(dp[n]!=-1)return dp[n];
vector<int> a = toVec(n);
int ans = INT_MAX;
for(int i=0;i<a.size();i++){
if(n-a[i]>=0 && a[i]>0)
ans = min(ans,1+find(n-a[i],dp));
}
return dp[n]=ans;
}
void solve(){
int n;
cin>>n;
vector<int> dp(n+1,-1);
cout<<find(n,dp);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
//memset(primes,true,sizeof(primes));
//sieve();
//memset(dp,false,sizeof(dp));
//precal();
/*int t;
cin>>t;
while(t--)*/
solve();
return 0;
}
//Tabulation Approach
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lld double
int mod=1e9+7;
const int array_size = 1e6+100;
bool primes[array_size];
ll gcd(ll a, ll b){if (b == 0)return a;return gcd(b, a % b);}
ll lcm(ll a, ll b){return ((a/gcd(a,b))*b);}
void sieve(){
primes[0] = false;
primes[1] = false;
for(ll i = 2;i*i<=array_size;i++){
if(primes[i]){
for(ll j=i*i;j<=array_size;j+=i)
primes[j] = false;
}
}
}
vector<int> toVec(int n){
vector<int> a;
while(n>0){
int r = n%10;
a.push_back(r);
n/=10;
}
return a;
}
void solve(){
int n;
cin>>n;
vector<int> dp(n+1,INT_MAX);
if(n<=9){
cout<<1<<endl;
return;
}
dp[0]=0;
for(int i=1;i<=9;i++){
dp[i]=1;
}
for(int i=10;i<=n;i++){
vector<int> a = toVec(i);
for(int j=0;j<a.size();j++){
if(a[j]==0) continue;
if(i-a[j]>=0)
dp[i] = min(dp[i],1+dp[i-a[j]]);
}
}
cout<<dp[n]<<endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
//memset(primes,true,sizeof(primes));
//sieve();
//memset(dp,false,sizeof(dp));
//precal();
/*int t;
cin>>t;
while(t--)*/
solve();
return 0;
}