Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- /** learnings
- _compare true means small false means large
- */
- int t[1000][5000];
- int p[1000];
- int makeNum(string s,int l,int r){
- int ans=0;
- for(int i=l;i<=r;i++){
- int pos = (int)(s[i]-'0');
- ans *= 10;
- ans += pos;
- }
- return ans;
- }
- int fun(string s,int sm,int l,int r){
- if(l==r+1)return INT_MAX-1;
- if(r-p[l]+1<=4){
- if(makeNum(s,p[l],r)==sm){
- return 0;
- }
- }
- int &res = t[l][sm];
- if(res!=-1)return res;
- int ans=INT_MAX-1;
- for(int k=p[l],cnt=1;(k<=r && cnt<=4);k++,cnt++){
- int pos = makeNum(s,p[l],k);
- int cur=INT_MAX-1;
- if(pos<=sm){
- cur=1+fun(s,sm-pos,k+1,r);
- ans = min(ans,cur);
- }
- }
- return res = ans;
- }
- void solve(){
- memset(t,-1,sizeof(t));
- string s;cin>>s;
- int sm=0,i,pd=0;
- int n = s.length();
- for(i=n-1;s[i]!='=';i--){
- int pos = (int)(s[i]-'0');
- if(pd==0)pd=1;
- else pd*=10;
- sm+=pos*pd;
- }
- p[i-1]=i-1;
- for(int j=i-2;j>=0;j--){
- p[j] = (s[j]=='0'?p[j+1]:j);
- }
- cout<<fun(s.substr(0,i),sm,0,i-1);
- }
- int main(){
- ios::sync_with_stdio(0);
- cin.tie(0);
- solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement